簡體   English   中英

刪除無文字的段落

[英]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>

不會被刪除

使用jQuery遍歷所有p個元素,然后僅獲取它的文本,對其進行修剪(這樣也將刪除空格),然后檢查其中是否有文本,如果沒有,則將其刪除。

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

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

好吧,如果您想使用javascript明智地進行前端操作,則可以這樣做。

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

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM