如何使用 jQuery 删除空的 p 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6092855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:23:04  来源:igfitidea点击:

How do I remove empty p tags with jQuery?

jquery

提问by Steve

The platform I'm building a website on produces empty ptags in wysiwyg mode. How can I take these out?

我正在构建网站的平台p以所见即所得模式生成空标签。我怎样才能把这些拿出来?

Something like this, perhaps...

像这样的东西,也许……

$("<p> </p>").remove();

$("<p> </p>").remove();

Although the code above does nothing.

虽然上面的代码什么也没做。

回答by mu is too short

The answer depends on what "empty" means. If the empty paragraphs are <p></p>then fireeyedboy's p:emptyselector 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></p>fireeyeedboy 的p:empty选择器,则是要走的路。如果可能有空格或换行符或其他类似的东西,那么你可能想要这样的东西:

$('p').each(function() {
    var $this = $(this);
    if($this.html().replace(/\s|&nbsp;/g, '').length == 0)
        $this.remove();
});

Example: http://jsfiddle.net/ambiguous/7L4WZ/

示例:http: //jsfiddle.net/ambiguous/7L4WZ/

FCKEditor (not sure about CKEditor or TinyMCE) likes to add <p>&nbsp;</p>to the HTML so you might need the above somewhat ugly approach.

FCKEditor(不确定 CKEditor 或 TinyMCE)喜欢添加<p>&nbsp;</p>到 HTML 中,因此您可能需要上述有点丑陋的方法。

回答by Decent Dabbler

Try:

尝试:

$( 'p:empty' ).remove();

$( 'p:empty' ).remove();

回答by Vivek

you can try this...

你可以试试这个...

$([selector]).is(":empty")   

it will return true if selector is empty..Working Demo

如果选择器为空,它将返回 true..工作演示

回答by ItsJonQ

I'm a little late to the party, but I found this thread recently as I was looking to solve this issue as well.

我参加聚会有点晚了,但我最近发现了这个帖子,因为我也想解决这个问题。

I came up with a Vanilla JS solution here, which worked for me:

我在这里想出了一个 Vanilla JS 解决方案,它对我有用:

var p = document.querySelectorAll('p:empty');
for(var i = p.length - 1; i > -1; i-- ) {
    p[i].parentNode.removeChild(p[i]);
}

It basically does (exactly) what fireeyedboy suggested, but without jQuery.

它基本上(完全)做了 fireeyedboy 的建议,但没有 jQuery。

It also appears to perform better than jQuery as well: http://jsperf.com/remove-empty-elements-javascript-vs-jquery

它的性能似乎也比 jQuery 好:http: //jsperf.com/remove-empty-elements-javascript-vs-jquery

Hope this helps!

希望这可以帮助!

回答by PHP Mentor

Thanks "mu is too short",

感谢“亩太短”,

I've tried your code It works but I need to wrap it in jQuery(document).ready(function() {});

我试过你的代码它有效,但我需要把它包装起来 jQuery(document).ready(function() {});

The full code worked for me is:

对我有用的完整代码是:

jQuery(document).ready(function() {
    jQuery('p').each(function() {
        var $this = jQuery(this);
        if($this.html().replace(/\s|&nbsp;/g, '').length == 0) {
            $this.remove();
        }
    });
});

I don't know why this happens, My jQuery/JS is not so good, I'm learning it ;).

我不知道为什么会发生这种情况,我的 jQuery/JS 不太好,我正在学习 ;)。

Hope this help another person like me.

希望这能帮助像我这样的人。

Thanks.

谢谢。

回答by Fabio Manzo

/* Remove empty paragraphs with &nbsp; */
jQuery('p').each(function(){
    if( jQuery(this).html() == '&nbsp;' )
        jQuery(this).remove();
})