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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:37:25  来源:igfitidea点击:

How to check if an input element is hidden?

jquery

提问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 typeattribute (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 :hiddenpseudoclass, but only one of them has the hiddentype. 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 nullor ""(the empty string);

getAttribute()Element 接口的方法返回元素上指定属性的值。如果给定的属性不存在,则返回的值将是null""(空字符串);

for a hidden element, you can check the typeattribute of element.

对于隐藏元素,您可以检查type元素的属性。

$('#myElement').getAttribute('type')=='hidden'