How do I remove empty p tags with jQuery?

后端 未结 6 890
后悔当初
后悔当初 2020-12-04 22:32

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

Something like this, perhaps...

$

6条回答
  •  长情又很酷
    2020-12-04 22:54

    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:

    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.

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

    Hope this helps!

提交回复
热议问题