代码之家  ›  专栏  ›  技术社区  ›  Santhoshkumar

如何删除summernote值中的标签?

  •  4
  • Santhoshkumar  · 技术社区  · 7 年前

    当我进入太空时,我从summernote textarea获得以下代码,

    <p><br></p><p><br></p><p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. </span><br></p><p><br></p><p><br></p>
    

    但我想去掉开头 <p><br></p><p><br></p> 在存储到数据库之前。

    例如,我想像下面这样存储,

    <p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. </span><br></p>
    
    7 回复  |  直到 5 年前
        1
  •  3
  •   frido    5 年前
    callbacks: {
      onFocus: function (contents) {
        if($('.summernote').summernote('isEmpty')){
          $(".summernote").html(''); 
        }
      }
    }
    
        2
  •  2
  •   jitender    7 年前

    如果你对Pattern有信心,那么你可以实现以下目标

    var data='<p><br></p><p><br></p><p><strong style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong><span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">&nbsp;is simply dummy text of the printing and typesetting industry. </span><br></p><p><br></p><p><br></p>';
    while(data.startsWith('<p><br></p>')){
    data=data.replace('<p><br></p>','')
    }
    
    while(data.endsWith('<p><br></p>')){
    data=data.replace(new RegExp('<p><br></p>$'),'')
    }
    console.log(data)
        3
  •  1
  •   IronFlare    5 年前

    使用以下选项:

    $('#summernote').summernote('code', '<text H>');
    

    而不是:

    $('#summernote').summernote('editor.insertText', '<text H>'));
    
        4
  •  1
  •   linktoahref Nimya V    3 年前

    最好的方法是:

    $('.summernote_add').summernote({
        height: 250,
        callbacks: {
            onChange: function (contents) {
                if ($('.summernote_add').summernote('isEmpty')) {
                    $(".add .panel-body").html('');
                } else {
                    $(".add .panel-body").val(contents);
                }
                // $('.summernote_add').val(
                //     $('.summernote_add').summernote('isEmpty')
                //         ? null
                //         : contents
                // );
                summernoteValidatorAdd.element($('.summernote_add'));
            }
        }
    });
    
        5
  •  0
  •   Ismail Farooq    7 年前

    只需删除空元素

    $("p").each(function(){
        if (!$(this).text().trim().length) {
            $(this).remove();
        }
    });
    

    https://jsfiddle.net/ox32tjwg/

        6
  •  0
  •   Egli Becerra    5 年前

    工作 为了我!刚才不得不这么做

    callbacks: {
      onFocus: function (contents) {
        if($(this).summernote('isEmpty')){
          $("<your-selector>").html(''); //either the class (.) or id (#) of your textarea or summernote element
        }
      }
    }
    
        7
  •  0
  •   Konrad Stępień    3 年前

    对于jQuery

    $('.summernote').summernote({
        height: 250,
        callbacks: {
            onInit: function (c) {
                c.editable.html('');
            }
        }
    });