【问题标题】:summernote doesn't allow to format pasted text after writing onpaste eventsummernote 不允许在编写 onpaste 事件后格式化粘贴的文本
【发布时间】:2016-02-21 08:39:04
【问题描述】:

我的问题是,当我在summernote 中粘贴数据时会清除格式,但如果我想按我的意愿格式化粘贴的文本,它不允许我更改粘贴的文本或我在编辑器中编写的文本. 我已使用此代码将 onpaste 事件绑定到 Summernote。

我正在使用 Summernote.js 版本 0.5.8

    $(".summernote").summernote({
            onpaste: function (e) {
            debugger;
            var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
            e.preventDefault();
            setTimeout(function () {
                document.execCommand('insertHTML', false, bufferText);
            }, 10);
        }
    })

【问题讨论】:

    标签: javascript summernote


    【解决方案1】:

    在 html 文件中使用 on-paste 属性

    <div summernote="rules" ng-model="league.rules" validate-summernote="checkLeagueRules" config="summernote_options" on-paste="removeFormatting(evt)" class="summernote"></div>

    在控制器中使用 removeFormatting 函数

    $scope.removeFormatting = function(e)
    { 
        var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
        e.preventDefault();
        // Firefox fix
        setTimeout(function () {            
            document.execCommand('insertText', false, bufferText);
        }, 10);  
    }
    

    【讨论】:

      【解决方案2】:

      解决了我的问题。我不得不销毁 Summernote 的对象并重新初始化它。我不确定这是否是正确的编码方式,但它对我有用。 当我在项目中的各个不同位置使用 Summernote 时,我在布局页面上执行了此操作

          $(".summernote").summernote({ height: 250 });
      
              $(".summernote").summernote({
                  onpaste: function (e) {
                      var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
                      e.preventDefault();
                      setTimeout(function () {
                          document.execCommand('insertText', false, bufferText);
                          $(this).parent().siblings('.summernote').destroy();                        
                      }, 10);
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 2020-03-20
        • 2022-06-23
        • 2023-03-29
        • 2021-09-29
        • 2018-07-09
        • 2019-12-17
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        相关资源
        最近更新 更多