如何使用jQuery从dom中删除空元素?

如何使用jQuery从dom中删除空元素?

问题描述:

WordPress单词编辑器会吐出空的<p>标记,如下所示:<p>&nbsp;</p>.

The WordPress word editor spits out empty <p> tags like this: <p>&nbsp;</p>.

我想将其中所有带有&nbsp;<p>定位为目标并将其删除.我相信我必须使用containsremove函数,但是我不确定该代码是否删除仅包含&nbsp;<p>标签或删除其中包含&nbsp;的所有<p>标签任何地方.

I would like to target all <p> with &nbsp; in them and remove them. I believe I have to use the contains and remove functions but I am not sure if this code would remove the <p> tags with only &nbsp; in them or remove all <p> tags with &nbsp; in them anywhere.

jQuery('p:contains("&nbsp;")').remove();

我将如何进行这项工作?

How would I make this work?

,您可以使用 .filter 并查看innerHTML是否为等于 :

you can use .filter and look if the innerHTML is equal to  :

$("p").filter(function(){
    return $.trim(this.innerHTML) === "&nbsp;"
}).remove();