Javascript QuillJS粘贴HTML无法正常工作
我有一个自定义按钮,它在羽毛笔工具栏中使用pasteHTML()。当我两次单击该按钮时,会出现一个错误:“无法读取未定义的属性'pasteHTML' 复制步骤Javascript QuillJS粘贴HTML无法正常工作,javascript,quill,Javascript,Quill,我有一个自定义按钮,它在羽毛笔工具栏中使用pasteHTML()。当我两次单击该按钮时,会出现一个错误:“无法读取未定义的属性'pasteHTML' 复制步骤 去 在编辑器中键入文本 点击按钮两次 注意:在JS设置(齿轮)下的codepen中,当我将JavaScript预处理器切换到babel时,它可以工作。不知道这到底是为什么 预期行为: 编辑器应切换回预览文本视图 实际行为: 它给出一个错误:“无法读取未定义的属性'pasteHTML' 平台: Windows 10,Chrome v62.0
// Code Preview
function showHtml() {
const txtArea = document.createElement('textarea')
txtArea.style.cssText = "width: 100%;margin: 0px;background: rgb(29, 29, 29);box-sizing: border-box;color: rgb(204, 204, 204);font-size: 15px;outline: none;padding: 20px;line-height: 24px;font-family: Consolas, Menlo, Monaco, "Courier New", monospace;position: absolute;top: 0;bottom: 0;border: none;display:none"
const htmlEditor = quill.addContainer('ql-custom')
htmlEditor.appendChild(txtArea)
const myEditor = document.querySelector('#editor')
quill.on('text-change', (delta, oldDelta, source) => {
const html = myEditor.children[0].innerHTML
txtArea.value = html
document.querySelector('.text-output').innerHTML = html
document.getElementById('html-output').innerText = html
})
const customButton = document.querySelector('.ql-showHtml');
customButton.addEventListener('click', function() {
if (txtArea.style.display === '') {
const html = txtArea.value
this.quill.pasteHTML(html)
}
txtArea.style.display = txtArea.style.display === 'none' ? '' : 'none'
})
}
在这一块
customButton.addEventListener('click', function() {
if (txtArea.style.display === '') {
const html = txtArea.value
this.quill.pasteHTML(html)
}
txtArea.style.display = txtArea.style.display === 'none' ? '' : 'none'
})
当您尝试引用
this.quill
,this
将引用customButton
+1 jmcgriz时,我只是自己想出来,然后返回SO查看您的评论。啊,是范围链!谢谢你,伙计!我会投票支持它,但没有足够的代表。只需从中删除“this”。quill.pasteHtml(html)应该可以,因为quill已经声明为全局常量。