如何检查输入元素是否被隐藏?

如何检查输入元素是否被隐藏?

隐藏为type="hidden"

 $("#myInputElement").attr('type') == 'hidden' 

隐藏为display: none

 $("#myInputElement").is(":hidden") 

nickf是正确的,但是对于由样式( style="display:none;" )或它的type属性( type="hidden" )隐藏的输入会产生相同的效果。
考虑以下html:

   

以上两个输入都有:hidden伪类,但只有一个具有hidden类型。 假设这是您正在寻找的(因为输入是唯一可以具有隐藏类型的元素,并且您想要检查输入元素是否被隐藏,而不仅仅是任何其他元素),对问题的正确答案是检查元素的type属性:

 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 

当然还有jquery方式:

 $('#first').attr('type') == 'hidden';// true $('#second').attr('type') == 'hidden';// false 

如果您只对普通可见性属性感兴趣,那么jquery伪类可以解决这个问题:

 $('#first').is(':hidden'); // or another way $('#first:hidden').length != 0;