![](/img/trans.png)
[英]How to scan and remove random characters in the end of paragraphs in UTF text
[英]Remove paragraphs with no text
有沒有一種方法可以刪除其中沒有文本的段落,但是其中可能有空的html標記?
例如
<p><strong><br></strong></p> or <p></p> or <p> </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.