How do I remove empty p tags with jQuery?

后端 未结 6 884
后悔当初
后悔当初 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 23:03

    The answer depends on what "empty" means. If the empty paragraphs are

    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();
    });
    

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

    FCKEditor (not sure about CKEditor or TinyMCE) likes to add

     

    to the HTML so you might need the above somewhat ugly approach.

提交回复
热议问题