How to clear content of mathquillified editable element?

让人想犯罪 __ 提交于 2019-12-12 03:22:28

问题


I'm using mathquill to render some latex. I have an editable mathquill element.

I would like to know how to programmatically clear the content.

When I manually clear it (backspace), everything works fine but if I remove elements from the DOM with jQuery it seems like some cache is used since I get old values contatenated with the new ones.

Can someone help me please ?

Thanks


回答1:


Trying to edit the DOM manually will break things. Use MathQuill's API instead, like so:

$('.mathquill-editable').mathquill('latex', '');

Change $('.mathquill-editable') to a more specific selector unless you want to clear every editable mathquill element on the page.

This will clear the content of textbox, and won't break it's function. However, the textbox will collapse into a tiny 6x6 pixel square, that won't expand to normal size unless the user clicks it. Certainly not dysfunctional, but not the prettiest either.

You can fix this by creating a custom key down event for the space key (source)

var space = $.Event('keydown');

space.bubbles = false;
space.cancelable = false;
space.charCode = 32;
space.keyCode = 32;
space.which = 32;

Note that the event is applied to mathquill's fake textarea on the interior, not the visible element itself.

$('.mathquill-editable').find('textarea').trigger(space);

Again, be sure the selector is specific to the element you wish to clear.

One last issue - the element won't be focused. Not sure if this is an issue for you, but the user will have to reselect the element to continue editing it. To fix that, you must apply the .focus() method:

$('.mathquill-editable').find('textarea').focus();



回答2:


maybe the API changed,I clear content of mathquill use the code as follow

//let MQ = MathQuill.getInterface(MathQuill.getInterface.MAX);
MQ.MathField($('.mathquill-editable')).latex("");



回答3:


You can put an id to the editable field like this:

<span id="main-input-solution">

After that you can create a function in javascript like this:

function clearContent(){
  var mathFieldSpan1 = document.getElementById('main-input-solution');
  mathFieldSpan1.innerHTML = "";
}

This function you can call e.g on button click event.



来源:https://stackoverflow.com/questions/34595741/how-to-clear-content-of-mathquillified-editable-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!