如何使用jQuery从dom中删除空元素? [英] How do I remove empty elements from the dom using jQuery?

查看:72
本文介绍了如何使用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();

这篇关于如何使用jQuery从dom中删除空元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆