如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?

如果元素可见或隐藏, 我如何测试 ?


当前回答

您可以使用此功能:

$(element).is(':visible');

示例代码

$( document) {( pready) {( ) {( ) {( ) $( ” tggle” ) . click () {( ) {( ) {( ) {( ) {( ) {( ) $( ) ) {( ) $( ) ) } . 点击 ( ) {( ) {( ) {( ” ) } } } } ; & ; 标注 src=" https://ajaax.googleapis.com/ax/libs/ jquery/ 12.2/ query. min. js> & () / statict> & pl= "content" & gt; this is a

其他回答

$( "div:visible" ).click(function() {
  $( this ).css( "background", "yellow" );
});
$( "button" ).click(function() {
  $( "div:hidden" ).show( "fast" );
});

api 文档: 可见选择器

人们可以简单地使用隐藏或可见属性,例如:

$('element:hidden')
$('element:visible')

或您可以简化相同内容如下。

$(element).is(":visible")
if($('#id_element').is(":visible")){
   alert('shown');
}else{
   alert('hidden');
}

您需要检查... 显示和可见度 :

if ($(this).css("display") == "none" || $(this).css("visibility") == "hidden") {
    // The element is not visible
} else {
    // The element is visible
}

如果我们自动检查$( thiss) 。 is (“: 可见”) , jquery check for both the things 自动检查 。

通常当检查某物是否可见时, 您会立即直接去做其他事情。 jquery 绑链很容易做到 。

所以,如果您有一个选择器, 并且您想要在它上执行某些动作, 只有当它为可见或隐藏时, 您可以使用过滤器 (“ : 可见 ” ) 或过滤器 (“ 隐藏 ” ) , 然后将它与您想要采取的行动连接起来 。

而不是这样声明:

if ($('#btnUpdate').is(":visible"))
{
     $('#btnUpdate').animate({ width: "toggle" });   // Hide button
}

或效率更高,但更丑陋:

var button = $('#btnUpdate');
if (button.is(":visible"))
{
     button.animate({ width: "toggle" });   // Hide button
}

您可以在一条线内完成全部任务:

$('#btnUpdate').filter(":visible").animate({ width: "toggle" });