删除没有文字的段落

Remove paragraphs with no text

有什么方法可以删除没有文本但可能有空 html 标签的段落?

例如

<p><strong><br></strong></p> or <p></p> or <p>&nbsp;</p>

会被删除但是

<p><strong>hi<br>there</strong></p> or <p>hi there</p>

不会被删除

好吧,如果你想在前端明智地使用 javascript 你可以这样做。

$(document).ready(function(){
    $('p').each(function(){
           if($(this).html().length == 0)
           {
               $(this).remove();
           }
    })

})

使用jQuery遍历所有p元素,然后只获取它的文本,trim它(所以只有空格也被删除),然后检查其中是否有文本, 如果没有,请将其删除。

$('p').each(function() {
    if ($(this).text().trim() == "") {
        $(this).remove();
    }
});

jsfiddle 示例:http://jsfiddle.net/ygbnpg77/