jQuery 如何检查输入元素是否隐藏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6966221/
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 check if an input element is hidden?
提问by clarkk
How can you check if an input element is hidden?
如何检查输入元素是否隐藏?
回答by nickf
Hidden as type="hidden"
隐藏为 type="hidden"
$("#myInputElement").attr('type') == 'hidden'
Hidden as display: none
隐藏为 display: none
$("#myInputElement").is(":hidden")
回答by gion_13
nickfis right, but that would have the same effect on an input that is hidden by style (style="display:none;"
) or by it's type
attribute (type="hidden"
).
Consider the following html :
nickf是对的,但这对由样式 ( style="display:none;"
) 或其type
属性 ( type="hidden"
)隐藏的输入具有相同的效果。
考虑以下 html :
<input id="first" type="hidden" />
<input id="second" type="text" style="display:none;"/>
Both of the above inputs have the :hidden
pseudoclass, but only one of them has the hidden
type. Assuming that this is what you were looking for (because inputs are the only elements that can have a hidden type and you wanted to check if an input elementis hidden, not just any other element), the right answer to you r question is to check the element's type attribute:
以上两个输入都有:hidden
伪类,但只有一个有hidden
类型。假设这是您要查找的内容(因为输入是唯一可以具有隐藏类型的元素,并且您想检查输入元素是否隐藏,而不仅仅是任何其他元素),您问题的正确答案是检查元素的类型属性:
document.getElementById('first').getAttribute('type') == 'hidden';// true
// although this input is also hidden, it wouldn't pass the check
document.getElementById('second').getAttribute('type') == 'hidden';// false
and , of course, the jquery way :
当然,还有 jquery 方式:
$('#first').attr('type') == 'hidden';// true
$('#second').attr('type') == 'hidden';// false
If you only were interested in the plain visibility property, then the jquery pseudoclass does the trick :
如果您只对普通的可见性属性感兴趣,那么 jquery 伪类可以解决问题:
$('#first').is(':hidden');
// or another way
$('#first:hidden').length != 0;
回答by devGirl
The getAttribute()
method of the Element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null
or ""
(the empty string);
getAttribute()
Element 接口的方法返回元素上指定属性的值。如果给定的属性不存在,则返回的值将是null
或""
(空字符串);
for a hidden element, you can check the type
attribute of element.
对于隐藏元素,您可以检查type
元素的属性。
$('#myElement').getAttribute('type')=='hidden'