代码之家  ›  专栏  ›  技术社区  ›  Dave Doga Oz

不同的JS语法-如何将这些属性添加到变量?

  •  0
  • Dave Doga Oz  · 技术社区  · 8 年前

    我有两个彼此相似的js代码。

    这个问题更多地与js相关,而不是与库相关。 我正在使用quilljs库实现一个文本编辑器,我可以在加载库之前自定义它的设置。

    我已经这样配置了库:

    var toolbarOptions = [
      [{ 'font': [] }],
      [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
      [{ 'align': [] }],
      ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
      [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
      ['blockquote', 'code-block'],
    
      [{ 'list': 'ordered'}, { 'list': 'bullet' }],
      [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
      [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
      [ 'link', 'video', 'formula' ], 
      ['clean']                                        // remove formatting button
    ];
    
    var quill = new Quill('#editor', {
      modules: {
        toolbar: toolbarOptions
      },
      placeholder: 'Compose a post...',
      theme: 'snow',
      'image-tooltip': true,
      'link-tooltip': true
    });
    
    $('#contenthidden').val(quill.root.innerHTML);
    
         );
    

    无论如何,我需要将下面的代码添加到toolbarOptions变量中,然后我必须加载quilljs等。。。 但它的内容完全不同。就Javascript而言,我无法理解如何将这个toolbarOptions变量的内容添加到上面的第一个变量中。

    var toolbarOptions = {
      handlers: {
        // handlers object will be merged with default handlers object
        'link': function(value) {
          if (value) {
            var href = prompt('Enter the URL');
            this.quill.format('link', href);
          } else {
            this.quill.format('link', false);
          }
        }
      }
    }
    

    感谢您的帮助。

    1 回复  |  直到 8 年前
        1
  •  1
  •   MinusFour    8 年前

    查看文档,似乎忽略了:

    toolbar : { container : containerOptions, handlers : handlerOptions } 
    

    做的只是:

    toolbar : containerOptions
    

    只是一条捷径 container 选择是如此普遍。因此,您必须使用第一个表单来指定处理程序选项。

    var toolbarOptions = {
        container: [
          [{ 'font': [] }],
          [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
          [{ 'align': [] }],
          ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
          [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
          ['blockquote', 'code-block'],
    
          [{ 'list': 'ordered'}, { 'list': 'bullet' }],
          [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
          [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
          [ 'link', 'video', 'formula' ], 
          ['clean']                                        // remove formatting button
        ],
      handlers: {
        // handlers object will be merged with default handlers object
        'link': function(value) {
          if (value) {
            var href = prompt('Enter the URL');
            this.quill.format('link', href);
          } else {
            this.quill.format('link', false);
          }
        }
      }
    }