[英]Using ':hidden' selector to find hidden elements in jquery
在jQuery中,我可以使用$(':hidden')
查找隱藏的元素,但是我不想包含0個size元素( 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>
我要選擇“ e2”,而不要選擇“ e1”。
在jQuery中使用過濾器
$(':hidden').filter(function(){
return $(this).width()!=0 && $(this).height()!=0;
});
像這樣嗎
if (element.is(':hidden') && element.width() != 0 && element.height != 0) {
//do some stuff
}
您可以嘗試使用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">
試試吧!!
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.