即时通讯使用自动生成空白html元素的cms在博客的帖子内容中:
<div class="post-list-item-content">
<div>
<h2 style="text-align: center;"></h2>
<h2> </h2>
<h2>What is Lorem Ipsum?</h2>
<h2 style="text-align: center;">
<img src="../img/img-001.jpg" alt="img-001.jpg" width="699" height="393">
</h2>
<p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</div>
我想用JQuery删除两个h2元素(一个是空的,另一个是空格)但是我必须检查那些h2是否为空或不是空白
答案 0 :(得分:1)
var emptyH2 = $('h2').filter(function(){
return $.trim($(this).text()).length == 0
});
emptyH2.remove();
<强>更新::
如果h2
标记包含嵌套元素,则可以使用带有一些正则表达式的html(),例如:
var emptyH2 = $('h2').filter(function() {
var $this = $(this);
return ($this.html().replace(/\s| /g, '').length == 0)
});
emptyH2.remove();
演示:: jsFiddle
答案 1 :(得分:0)
您可以尝试这样的事情,
if($("h2").html().trim() == ""){
$("h2").empty();
//OR
$("h2").remove() // will remove complete DOM
}
答案 2 :(得分:0)
试试这个:
$('h2').filter(function(i,e){ return $.trim($(e).text()) == ''}).remove();