简体   繁体   English

Quill文本编辑器如何在使用JQuery创建后使用实例

[英]Quill text editor how to use instances after creating with JQuery

I'm using Quill text editor as the text editor of my site, one of the site options is to create dynamic text editors, the function that defines the quill editor looks like that: 我使用Quill文本编辑器作为我网站的文本编辑器,其中一个站点选项是创建动态文本编辑器,定义主轴编辑器的功能如下所示:

function quillDefine(quillID) //quillID is the id of the div that I'm going to define as quill element
{
    var toolbarOptions = 
        [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote'],
            [{'align':[]}],
            ['clean']
        ]

    var quill = new Quill('#'+quillID, {
        modules: {toolbar: toolbarOptions},
        theme: 'snow'
    });
}

how can I use this quill instance that I created inside other funcitons? 我怎样才能使用我在其他函数中创建的这个quill实例? for example: 例如:

funciton getQuillInstanceText(quillID)
{
    //I know I can use $('#'quillID).html(), but is that the proper way to do so?
}

I want to use the quill API functions like getContents() and etc. 我想使用quill API函数,如getContents()等。

How can I reach to the instance I just created? 我怎样才能到达刚刚创建的实例? Thanks a lot. 非常感谢。

Unfortunately there is no Quill API that will return a Quill instance from a corresponding DOM container. 不幸的是,没有Quill API会从相应的DOM容器中返回Quill实例。 If you are using jQuery you can use $.data(): 如果您使用的是jQuery,则可以使用$ .data():

var quill = new Quill(quillID);
$(quillID).data("quill", quill);

funciton getQuillInstanceText(quillID) {
  var quill = $(quillID).data("quill");
  var text = quill.getText();
  return text;
}

Or with plain JS: 或者使用简单的JS:

var container = document.querySelector(quillID);
var quill = new Quill(container);
container.__quill = quill;

funciton getQuillInstanceText(quillID) {
  var container = document.querySelector(quillID);
  var quill = container.__quill;
  var text = quill.getText();
  return text;
}

Edit: As of Quill 1.2.0, you can now use the experimental find API. 编辑:从Quill 1.2.0开始,您现在可以使用实验性查找 API。

Instead of saving the editor object as an attribute of the HTML, you can also create an object that will hold all editors, and you can store and get each editor by key (ID) whenever you want: 您可以创建一个包含所有编辑器的对象,而不是将编辑器对象保存为HTML的属性,您可以随时按键(ID)存储和获取每个编辑器:

var editors={};
editors[quilID] = new Quill(quilID);

function getQuillInstanceText(quillID) {
  var quill = editors[quillID];
  var text = quill.getText();
  return text;
}

In this case you don't need any DOM querying nor JQuery. 在这种情况下,您不需要任何DOM查询或JQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM