如何使用jQuery从dom中删除空元素?
问题描述:
WordPress单词编辑器会吐出空的<p>
标记,如下所示:<p> </p>
.
The WordPress word editor spits out empty <p>
tags like this: <p> </p>
.
我想将其中所有带有
的<p>
定位为目标并将其删除.我相信我必须使用contains
和remove
函数,但是我不确定该代码是否删除仅包含
的<p>
标签或删除其中包含
的所有<p>
标签任何地方.
I would like to target all <p>
with
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
in them or remove all <p>
tags with
in them anywhere.
jQuery('p:contains(" ")').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) === " "
}).remove();