简体   繁体   English

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

[英]Using ':hidden' selector to find hidden elements in jquery

In jQuery, I can use $(':hidden') to find hidden elements, but I don't want to include the 0 size elements ( width=0 and height=0 ). 在jQuery中,我可以使用$(':hidden')查找隐藏的元素,但是我不想包含0个size元素( width=0 and height=0 )。

How to achieve this? 如何实现呢?

I can't just judge if it is 0 size, because if one of its parents is hidden, I also want to select it. 我不能仅仅判断它的大小是否为0,因为如果其父项之一被隐藏,我也想选择它。

For example: 例如:

 <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> 

I want to select "e2",but not "e1". 我要选择“ e2”,而不要选择“ e1”。

use filter in jquery 在jQuery中使用过滤器

$(':hidden').filter(function(){
      return $(this).width()!=0 && $(this).height()!=0;
});

Something like this? 像这样吗

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

You can try using the not method or not selector of jQuery to solve your issue. 您可以尝试使用jQuery的not方法或not选择器来解决您的问题。 For example, 例如,

 $(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"> 

Try it!! 试试吧!!

If you set the style attribute to display: none; 如果将样式属性设置为display: none; (or use jQuery's .hide()) you can use this (或使用jQuery的.hide()),您可以使用此

( Demo ) 演示

$('*[style*="display: none;"]');
jQuery('elem:hidden').each(function(ind){
if(jQuery(this).height() > 0 && jQuery(this).width() > 0){
console.log(jQuery(this))
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM