如何清除所见即所得的mathquill编辑器?

How to clear the WYSIWYG mathquill editor?

在这个地址:http://jenseng.github.io/mathquill/demo.html

我打开网络控制台并输入:

$(document.getElementsByClassName("mathquill-editor")).mathquill('latex','')

它成功地清除了页面底部的 WYSIWYG 编辑器,但是我无法输入任何其他内容。它还将光标从左侧移动到右侧。任何想法如何使文本框可编辑并再次将光标移动到左侧?

这似乎是因为当您 运行 .mathquill('latex', info) 时,它还会删除使用编辑器所必需的元素。编辑器 ($('.mathquill-editor')) 需要它的前 2 个 children 才能运行,其余的是 mathquill 的一部分。

主要清除方法如下:

while ($('.mathquill-editor').children().length > 2)
    $('.mathquill-editor').children()[2].remove();
$('.mathquill-editor').addClass('empty');

但我也会包括一个更动态的方式,以防上面的代码在另一个网站上不起作用and/or类名不一样。

JQuery

function clearEditor(elem) {
    while ($(elem).children().length > 2)
        $(elem).children()[2].remove();
    $(elem).addClass('empty');
}
clearEditor('.mathquill-editor');

纯粹Javascript

function clearEditor(elem) {
    while (document.querySelector(elem).children.length > 2)
        document.querySelector(elem).children[2].remove();
    document.querySelector(elem).setAttribute('class', document.querySelector(elem).getAttribute('class') + ' empty');
}
clearEditor('.mathquill-editor');

或者您可以告诉编辑器执行哪些击键。

mathquillEditor.focus();

mathquillEditor.keystroke('End Shift-Home Del');