Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery删除空的p标记?_Jquery - Fatal编程技术网

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

如何使用jQuery删除空的p标记?,jquery,Jquery,我正在构建网站的平台以所见即所得模式生成空的p标记。我怎样才能把这些拿出来 像这样的事情,也许 $(“”)删除() 尽管上面的代码没有任何作用。请尝试: $('p:empty')。删除()你可以试试这个 $([selector]).is(":empty") 如果选择器为空,则返回true。答案取决于“空”的含义。如果空段落是,那么fireeyedboy的p:empty选择器就是好办法。如果可能有空格、换行符或其他类似的东西,那么您可能会想要这样的东西: $('p').each(funct

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

像这样的事情,也许

$(“

”)删除()

尽管上面的代码没有任何作用。

请尝试:

$('p:empty')。删除()

你可以试试这个

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

如果选择器为空,则返回true。

答案取决于“空”的含义。如果空段落是

,那么fireeyedboy的
p:empty
选择器就是好办法。如果可能有空格、换行符或其他类似的东西,那么您可能会想要这样的东西:

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


FCKEditor(不确定是否使用CKEditor或TinyMCE)喜欢将

添加到HTML中,因此您可能需要上述有点难看的方法。

我参加聚会有点晚,但我最近发现了此线程,因为我也在寻求解决此问题

我在这里提出了一个普通的JS解决方案,它对我很有效:

var p = document.querySelectorAll('p:empty');
for(var i = p.length - 1; i > -1; i-- ) {
    p[i].parentNode.removeChild(p[i]);
}
它基本上(准确地)实现了fireeyedboy的建议,但没有jQuery

它的性能似乎也比jQuery好:

希望这有帮助

谢谢“穆太短了”

我已经尝试了你的代码,它可以工作,但是我需要将它包装在
jQuery(document).ready(function(){})中

为我工作的完整代码是:

jQuery(document).ready(function() {
    jQuery('p').each(function() {
        var $this = jQuery(this);
        if($this.html().replace(/\s| /g, '').length == 0) {
            $this.remove();
        }
    });
});
我不知道为什么会发生这种情况,我的jQuery/JS不太好,我正在学习;)

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


谢谢。

如果有人需要这个WP站点,请使用这些:

/* Remove empty paragraphs with   */
jQuery('p').each(function(){
    if( jQuery(this).html() == ' ' )
        jQuery(this).remove();
})
jQuery('p').each(function() {
        var $this = jQuery(this);
        if($this.html().replace(/\s| /g, '').length == 0)
            $this.remove();
    });

querySelectorAll存在的问题是,较旧的browsersGood point不支持它。我想在这种情况下,如果您使用querySelector选择香草路线,您可以使用Polyfill添加较旧的浏览器支持。最佳且简单