[英]How to remove all empty or space div on page?
我有簡單的基本html結構
<div class="fl-no-pad" style="display: block;">
</div>
你看到這個div是空的還是有空的。在這種情況下我可以刪除所有的theese div嗎?
這是我的jquery代碼
$(".fl-no-pad").each(function() {
if ($(this).is(':empty')){
$(this).remove();
}
});
你可以在jQuery中使用filter
。 這樣您就可以為“空”設置自己的定義。
$(function() { var empties = $('div').filter(function(){ return $(this).html().trim() == ''; }); alert(empties.length); empties.remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fl-no-pad" style="display: block;"> </div>
為了符合futur CSS4規范 ,你可以擴展jQuery偽選擇器:
$.extend($.expr[':'],{
blank: function(elm) {
return !$(elm).html().trim().length;
}
});
然后使用:
$('div:blank').remove();
僅在CSS4中,您無法顯示元素:
div:blank { display: none; }
嘗試使用類似的東西:
if( !$.trim( $(this).html() ).length ) {
//do something
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.