jQuery 如何知道jquery中div的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5673108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know the status of the div in jquery?
提问by user601367
i am developin one application using jquery
. i want to know the status of the div
wheather the div
is show state or hide state
something like this:
我正在开发一个使用jquery
. 我想知道的地位div
wheather的div
是显示状态还是隐藏状态是这样的:
if($("#test").show()==true)
{
//some operration
}
else
{
//some operration
}
alert($("#test").show()==true);
always shows false
.
alert($("#test").show()==true);
总是显示false
。
please help me...
请帮我...
回答by AndrewR
You can use is() and the :visible selector.
您可以使用 is() 和 :visible 选择器。
if( $('#test').is(':visible') ) { ... }
回答by diEcho
try
尝试
$(element).is(":visible")
Reference
Note: hidden fails on elements that have width but no height.
回答by lonesomeday
is(':visible')
is, of course, correct.
is(':visible')
当然是正确的。
In pretty much all my jQuery applications, I introduce a simple plugin isVisible
.
在我几乎所有的 jQuery 应用程序中,我都引入了一个简单的插件isVisible
。
$.fn.isVisible = function() {
return $.expr.filters.visible(this[0]);
};
This is about 50 times faster than the above function (jsPerf example) for exactly the same functionality.
对于完全相同的功能,这比上述函数(jsPerf 示例)快约 50 倍。
if ($('#yourElement').isVisible()) {