如何使用jQuery删除空的p标签? [英] How do I remove empty p tags with jQuery?
问题描述
我在其上构建网站的平台在所见即所得模式下产生空的p
标签.我该如何取出这些东西?
The platform I'm building a website on produces empty p
tags in wysiwyg mode. How can I take these out?
也许是这样...
$("<p> </p>").remove();
尽管上面的代码什么也没做.
Although the code above does nothing.
推荐答案
答案取决于空"的含义.如果空白段落为<p></p>
,则使用fireeyedboy的p:empty
选择器.如果可能有空格或换行符或其他类似的东西,那么您可能会想要这样的东西:
The answer depends on what "empty" means. If the empty paragraphs are <p></p>
then fireeyedboy's p:empty
selector is the way to go. If there could be spaces or newlines or other such things then you'll probably want something like this:
$('p').each(function() {
var $this = $(this);
if($this.html().replace(/\s| /g, '').length == 0)
$this.remove();
});
示例: http://jsfiddle.net/ambiguous/7L4WZ/
FCKEditor(不确定CKEditor还是TinyMCE)喜欢在HTML中添加<p> </p>
,因此您可能需要上面的方法有点难看.
FCKEditor (not sure about CKEditor or TinyMCE) likes to add <p> </p>
to the HTML so you might need the above somewhat ugly approach.
这篇关于如何使用jQuery删除空的p标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!