使用':隐藏'选择器在jquery中查找隐藏的元素

时间:2015-05-29 06:30:25

标签: javascript jquery hidden

在jQuery中,我可以使用$(':hidden')来查找隐藏的元素,但我不想包含0个大小的元素(width=0 and height=0)。

如何实现这一目标?

我无法判断它是否为0大小,因为如果其中一个父母被隐藏,我也想选择它。

例如:



<img id="e1" width="0" height="0" />
<div style="display:none;">
    <img id="e2" width="0" height="0" />
</div>

<script>
    // I want to select "e2",but not "e1"
    var myelems = $('img:hidden');
</script>
&#13;
&#13;
&#13;

我想选择&#34; e2&#34;但不是&#34; e1&#34;。

5 个答案:

答案 0 :(得分:0)

在jquery中使用 filter

__getitem__

答案 1 :(得分:0)

这样的东西?

if (element.is(':hidden') && element.width() != 0 && element.height != 0) {
   //do some stuff
}

答案 2 :(得分:0)

如果将style属性设置为display: none;(或使用jQuery&#39; s .hide()),则可以使用此

Demo

$('*[style*="display: none;"]');

答案 3 :(得分:0)

jQuery('elem:hidden').each(function(ind){
if(jQuery(this).height() > 0 && jQuery(this).width() > 0){
console.log(jQuery(this))
}
});

答案 4 :(得分:-1)

您可以尝试使用jQuery的not方法或not选择器来解决您的问题。 例如,

$(document).ready(function() {
  console.log($(":hidden").not($("input[width=0],input[height=0]")));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Check the output in console</h1>
<input type="hidden" name="a1" id="a1" value="a1">
<input type="hidden" name="a2" id="a2" value="a2">
<input type="hidden" name="b1" id="b1" width=0 height=0 value="a3">

试一试!!