[英]How to access the innerHTML of multiple Quill instances with the same class name?
I am trying to have multiple Quill text editor instances on a single page, I achieved that but now I am straggling on how to get the innerHTML of each.我试图在一个页面上有多个 Quill 文本编辑器实例,我实现了,但现在我在如何获取每个的 innerHTML 上犹豫不决。 To create a single instance and get its innerHTML and assign it to a hidden input, I use the following:
要创建单个实例并获取其 innerHTML 并将其分配给隐藏的输入,我使用以下内容:
// CREATE A QUILL INSTANCE
var quill = new Quill('#editor-container', {
modules: {
toolbar: [
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'size': ['small', false, 'large', 'huge']}],
[{'font': [] }],
['bold', 'italic', 'underline', 'strike'],
['link', 'blockquote', 'code-block', 'image', 'video'],
]
},
placeholder: 'Compose an epic...',
theme: 'snow'
});
// GET THE innerHTML OF THE QUILL INSTANCE ANS ASSIGN IT TO A HIDDEN FIELD.
var form = document.getElementById("writeArticleForm");
form.onsubmit = function() {
// Populate hidden form on submit
var articlebody = document.querySelector('input[name=articleBody]');
var html = document.querySelector(".ql-editor").innerHTML;
articlebody.value = html;
return true;
}
But when I create for instance, two instances of QUILL, how could I use the querySelector to get the innerHTML of each instance and assign it to a variable?但是,例如,当我创建两个 QUILL 实例时,我如何使用 querySelector 获取每个实例的 innerHTML 并将其分配给一个变量?
You can usequerySelectorAll
to get all elements that match a class name.您可以使用
querySelectorAll
来获取与 class 名称匹配的所有元素。 To iterate over the resulting NoteList
you'll have to convert it to an array, my method of choice is using the spread operator .要遍历生成的
NoteList
,您必须将其转换为数组,我选择的方法是使用扩展运算符。
document.querySelector("#read_button").addEventListener("click", () => { let output = document.querySelector("#output"); let form = document.querySelector("#form"); output.innerHTML = ""; form.innerHTML = ""; // Get all inputs let elements = document.querySelectorAll(".input"); // Spread NodeList into array and iterate [...elements].forEach((input, index) => { output.innerHTML += `<li>${input.innerHTML}</li>`; form.innerHTML += `<input type="hidden" id="input${index}_value" value="${input.innerHTML}">`; }); });
.input { border: 1px solid black; padding: 0.25em; } #read_button { margin-top: 1em; }
<textarea class="input">Input 1</textarea> <textarea class="input">Input 2</textarea> <textarea class="input">Input 3</textarea> <textarea class="input">Input 4</textarea> <button id="read_button"> Read Inputs </button> <p>Output:</p> <ul id="output"></ul> <form id="form"></form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.