如何删除summernote值中的<p><br></p>标签?

问题描述 投票:0回答:9
html angularjs summernote
9个回答
3
投票
callbacks: {
  onFocus: function (contents) {
    if($('.summernote').summernote('isEmpty')){
      $(".summernote").html(''); 
    }
  }
}

2
投票

如果您确定模式,那么您可以实现类似的

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)


1
投票

使用以下内容:

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

代替:

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

1
投票

最好的方法是这样做:

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

1
投票

对于 jQuery

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

0
投票

只需删除空元素

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

https://jsfiddle.net/ox32tjwg/


0
投票

这对我来说有用!必须现在就做

callbacks: { onFocus: function (contents) { if($(this).summernote('isEmpty')){ $("<your-selector>").html(''); //either the class (.) or id (#) of your textarea or summernote element } } }
    

0
投票
实现这个回调。您将剩下一对

<p><be></p>

,根据文档,这很好。 OnBlur,您只需重置 Summernote,以便它将删除所有其他剩余标签。

请参阅此处的文档:

为空 返回编辑器内容是否为空。 即使编辑器内容为空,编辑区域也需要 <p><br></p>

 才能获得焦点。所以Summernote支持这个方法来帮助检查编辑器内容是否为空。

onBlur: function (contents) { if ( $($('#body-summernote').summernote('code')).text().trim().length < 1 ) { console.log( $($('#body-summernote').summernote('code')).text().trim().length ); console.log('clearing'); // $('#body-summernote').html(''); $('#body-summernote').summernote('reset'); } },
    

0
投票
如果您只想删除前导/开始标签,请将其与正则表达式一起使用,否则它还会删除不是前导/开始标签的其他标签,您还可以使用单循环删除结束/训练标签。

let value='<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(value.startsWith('<p><br></p>') || value.endsWith('<p><br></p>')){ value=value.replace(new RegExp('^<p><br></p>'),'').trim(); value=value.replace(new RegExp('<p><br></p>$'),'').trim(); }
    
© www.soinside.com 2019 - 2024. All rights reserved.