How do I check if an element is hidden in jQuery?

It is possible to toggle the visibility of an element, using the functions .hide() , .show() or .toggle() .

How would you test if an element is visible or hidden?


Since the question refers to a single element, this code might be more suitable:

// Checks css for display:[none|block], ignores visibility:[true|false]
$(element).is(":visible"); 

Same as twernt's suggestion, but applied to a single element; and it matches the algorithm recommended in the jQuery FAQ


You can use the hidden selector:

// Matches all elements that are hidden
$('element:hidden')

And the visible selector:

// Matches all elements that are visible
$('element:visible')

if ( $(element).css('display') == 'none' ){
    // element is hidden
}

函数不能与可见性属性一起使用。

链接地址: http://www.djcxy.com/p/28.html

上一篇: 为什么不能(或不)编译器将可预测的加法循环优化为乘法?

下一篇: 我如何检查一个元素是否隐藏在jQuery中?