【问题标题】:Summernote change height value after initial setupSummernote 初始设置后更改高度值
【发布时间】:2019-04-09 19:09:06
【问题描述】:

按照预期,我在我的网站上使用了summernote:

    $('#summernote').summernote({
        height: $(document).height() - ($("#Maintable").height() + $("#TblTop").height() + 60),
        minHeight: null,             // set minimum height of editor
        maxHeight: null,             // set maximum height of editor
        focus: true,

        toolbar: [
            // [groupName, [list of button]]
            ['style', ['fontname', 'bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize', 'undo', 'redo']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['insert', ['picture', 'video', 'table']],
            ['search', ['findnreplace', 'changecolor']]
        ],
        buttons: {
            changecolor: ChangeColorButton
        }
    });

但是,当我在 window.onresize 上调用的函数中调用以下代码以更改高度值时,什么也没有发生。我该怎么办?

   $('#summernote').summernote({
                height: 100     
   });

【问题讨论】:

    标签: javascript summernote


    【解决方案1】:

    documentation看来,summernote似乎没有提供api来设置初始化后的高度。

    但是,通过检查创建的编辑器的 html 结构,heightminHeightmaxHeight 选项将应用于 divclass="note-editable"。所以我们改为设置这个div 的高度。以下代码 sn -p 展示了如何在初始化后更改编辑器的高度。

    $(document).ready(function() {
      var t = $('#summernote').summernote(
      {
      height: 100,
      focus: true
    }
      );
      $("#btn").click(function(){
        $('div.note-editable').height(150);
      });
    });
    <!-- include libraries(jQuery, bootstrap) -->
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 
    
    <!-- include summernote css/js -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>
    <div id="summernote">Original height is 100. Click the button to change the height to 150</div>
    <button id="btn">Change Height</button>

    【讨论】:

      【解决方案2】:

      只能用 JS 来完成,但它是一个讨厌的解决方法:

      let summernoteOptions = {
              height: 300
          }
      
      $('#summernote').summernote(summernoteOptions);
      
      $(document).on('click', '#change', function(){
      
      summernoteOptions.height = 100;
      
        let content = $('#summernote').summernote('code');
      
        $('#summernote').summernote('destroy');
        $('#summernote').summernote(summernoteOptions);
        $('#summernote').summernote('code', content);
      
      });
      <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
      <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
      <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 
      
      <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
      <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>
      <div id="summernote">Some text...</div>
      <button id="change">Change Height Button</button>

      【讨论】:

      • 这非常有效。我添加了一个按钮来在滚动模式和全屏之间切换,这是实现它的完美方式。谢谢!
      猜你喜欢
      • 2014-12-19
      • 2011-05-04
      • 2019-01-24
      • 1970-01-01
      • 2013-11-15
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      相关资源
      最近更新 更多