我在博客的帖子内容中使用自动生成空白 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 是空还是非空
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句