【问题标题】:Quill Editor - unable to set data on clickQuill Editor - 无法在点击时设置数据
【发布时间】:2019-09-03 20:20:36
【问题描述】:

我正在使用 quill 编辑器并在引导模式内部,它在以 JSON 形式在 data-* 属性中插入数据时工作正常

那么问题出在哪里:

问题是当我尝试使用 setContent api 在羽毛笔编辑器中设置数据时它不起作用

/*
        ====================
            Quill Editor
        ====================
    */

    var quill = new Quill('#editor-container', {
      modules: {
        toolbar: [
          [{ header: [1, 2, false] }],
          ['bold', 'italic', 'underline'],
          ['image', 'code-block']
        ]
      },
      placeholder: 'Compose an epic...',
      theme: 'snow'  // or 'bubble'
    });

这是我的jsfiddle(使用前请阅读以下说明)

如何使用我的小提琴:

  1. 点击添加元素按钮。
  2. 在羽毛笔编辑器中输入任何内容
  3. 点击保存更改按钮。
  4. 现在您将看到一个使用 js 动态添加的元素
  5. 检查动态元素。您将看到带有 quill json 数据的 data-text 属性。
  6. 现在,单击动态元素,它会打开模式现在出现问题,它没有设置它正在获取表单数据-* 属性的内容值。

【问题讨论】:

    标签: javascript json twitter-bootstrap quill


    【解决方案1】:

    诀窍是将 JSON 字符串转换回 JSON 对象:


    来自这个var dataText = $(this).attr('data-text');

    致此var dataText = JSON.parse($(this).attr('data-text'));


    Updated Fiddle


    var quill = new Quill('#editor-container', {
      modules: {
        toolbar: [
          [{
            header: [1, 2, false]
          }],
          ['bold', 'italic', 'underline'],
          ['image', 'code-block']
        ]
      },
      placeholder: 'Compose an epic...',
      theme: 'snow'
    });
    
    function modalclick() {
      $(".dynamic-element").on('click', function(event) {
        var dataText = JSON.parse($(this).attr('data-text'));
        $('#exampleModalLong').modal('show');
        quill.setContents(dataText);
        console.log(dataText);
      })
    }
    
    $("#addElement").on('click', function(event) {
      var delta = quill.getContents();
      var $_textDelta = JSON.stringify(delta);
      console.log($_textDelta);
      $html = "<div class='dynamic-element' data-text='" + $_textDelta + "'>" +
        "<div>dynamic-element</div>" +
        "</div>";
      $(".element").prepend($html);
      modalclick();
      $('#exampleModalLong').modal('hide');
    })
    
    //modalclick();
    
    $("#openmodal").on('click', function(event) {
      $('#exampleModalLong').modal('show');
    })
    
    $('#exampleModalLong').on('hidden.bs.modal', function(e) {
      quill.deleteText(0, 2000);
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet"/>
    
    
    <!-- Button trigger modal -->
    <input type="button" id="openmodal" class="btn btn-info btn-rounded mb-4" value="Add element">
    
    <div class="element">
    
    </div>
    
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <div id="editor-container">
    
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" id="addElement" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 非常感谢。你救了我
    • 很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 2019-03-30
    • 1970-01-01
    • 2020-11-06
    相关资源
    最近更新 更多