最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove <p>  <p> with jquery if no value - Stack Overflow

programmeradmin1浏览0评论

how to hide attributes if detect this <p> &nbsp;</p>

My problem is when my client insert data(example table) with ckeditor , when i see the source code , ckeditor will add this <p> &nbsp;</p> after table code. i know how to remove this manualy with source code(open source code and delete) but not my client!

how to hide attributes if detect this <p> &nbsp;</p>

My problem is when my client insert data(example table) with ckeditor , when i see the source code , ckeditor will add this <p> &nbsp;</p> after table code. i know how to remove this manualy with source code(open source code and delete) but not my client!

Share Improve this question edited Apr 19, 2012 at 11:09 Pranay Rana 177k37 gold badges243 silver badges265 bronze badges asked Apr 19, 2012 at 10:14 ruslyrossiruslyrossi 3661 gold badge6 silver badges21 bronze badges 2
  • have you tried answer ? is it work for you ? – Pranay Rana Commented Apr 19, 2012 at 10:34
  • WYSIWYG editors do this so that when someone clicks on the bottom of the text editor it gives them an actual place to put their cursor. Say if there's a table, the only other place to put the cursor would be inside of the table, and that's probably not what people want when using a WYSIWYG editor. – Kirkland Commented Aug 11, 2015 at 17:56
Add a comment  | 

5 Answers 5

Reset to default 13

Orignal answer : How do I remove empty p tags with jQuery?

Try

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

here is working code : http://jsfiddle.net/ambiguous/7L4WZ/

I think this should work. Pretty quick and hacky

$("p").each(function() { 
   var $el = $(this);
   if($.trim($el.html()) == "&nbsp;") {
     $el.remove();
   }
 });
$('p').each(function(){
    var value = $.trim($(this).html());
    if(value == '&nbsp;'){
        $(this).remove();
    }
});

this will work on all p tags so better to write selector with its parent tag, so it should not effect other page elements.

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

This worked like a charm for me. Thank you Pranay!

$('p').filter(function() {
 return trim($(this).text()) == "";
 }).remove(); 
发布评论

评论列表(0)

  1. 暂无评论