使用 jQuery 检查元素是否为“display:none”或阻止点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15924751/
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, using jQuery, if an element is 'display:none' or block on click
提问by Nicholas Francis
I want to check and sort elements that are hidden. Is it possible to find all elements with attribute display
and value none
?
我想检查和排序隐藏的元素。是否可以找到所有具有属性display
和值的元素none
?
回答by Adil
You can use :visiblefor visible elements and :hiddento find out hidden elements. This hidden elements have display
attribute set to none
.
您可以对可见元素使用:visible ,对可见元素使用: hidden来找出隐藏元素。此隐藏元素的display
属性设置为none
。
hiddenElements = $(':hidden');
visibleElements = $(':visible');
To check particular element.
检查特定元素。
if($('#yourID:visible').length == 0)
{
}
Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero, Reference
如果元素占用文档中的空间,则元素被认为是可见的。可见元素的宽度或高度大于零, 参考
You can also use is()with :visible
您还可以将is()与:visible
if(!$('#yourID').is(':visible'))
{
}
If you want to check value of display then you can use css()
如果要检查显示值,则可以使用css()
if($('#yourID').css('display') == 'none')
{
}
If you are using display the following values display
can have.
如果您使用显示,则display
可以有以下值。
display: none
display: inline
display: block
display: list-item
display: inline-block
显示:无
显示:内联
显示:块
显示:列表项
显示:内联块
Check complete list of possible display
values here.
在此处检查可能display
值的完整列表。
To check the display property with JavaScript
使用 JavaScript 检查显示属性
var isVisible = document.getElementById("yourID").style.display == "block";
var isHidden = document.getElementById("yourID").style.display == "none";
回答by Deepanshu Goyal
$("element").filter(function() { return $(this).css("display") == "none" });
回答by jjhavokk
Yes, you can use the cssfunction. The below will search all divs, but you can modify it for whatever elements you need
是的,您可以使用 cssfunction。下面将搜索所有 div,但您可以针对您需要的任何元素修改它
$('div').each(function(){
if ( $(this).css('display') == 'none')
{
//do something
}
});
回答by Rana
Use this condition:
使用这个条件:
if (jQuery(".profile-page-cont").css('display') == 'block'){
// Condition
}
回答by Luceos
There are two methods in jQuery to check for visibility:
jQuery 中有两种方法可以检查可见性:
$("#selector").is(":visible")
and
和
$("#selector").is(":hidden")
You can also execute commands based on visibility in the selector;
您还可以根据选择器中的可见性执行命令;
$("#selector:visible").hide()
or
或者
$("#selector:hidden").show()
回答by Barry Chapman
$('#selector').is(':visible');