jQuery 使用javascript检查DIV是否可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16826515/
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
Check if DIV is visible using javascript
提问by Eytch
i needed to hide a div on my code behind:
我需要在我的代码后面隐藏一个 div:
bool hideDiv = false
//codes to change hideDiv
myDiv.visible = hideDiv;
and i want to check visibility of my div using javascript:
我想使用javascript检查我的div的可见性:
if (jQuery("myDiv") != null){
//some codes
}
else{
//some codes
}
and the 'jQuery("myDiv")' is always not null (even if the div was actually not visible), what is the better way to check if a div is visible?
并且 'jQuery("myDiv")' 始终不为空(即使 div 实际上不可见),检查 div 是否可见的更好方法是什么?
回答by gdoron is supporting Monica
You can use :visible
selectorinside is
filtering function:
您可以在过滤功能中使用:visible
选择器:is
if ($('#myDiv').is(':visible'))
Notes:
笔记:
- You probably forgot the
#
before the id in your selector(jQuery("myDiv")
). - jQuery will never return null no matter if the searched elements exist or not, unlike
document.getElementById
- 您可能忘记了
#
选择器 (jQuery("myDiv")
) 中的 id 之前。 - 无论搜索的元素是否存在,jQuery 都不会返回 null,不像
document.getElementById