简体   繁体   English

如何杀死CodeMirror实例?

[英]How to kill a CodeMirror instance?

I'm using Codemirror v3.16 and I'm trying to figure out how I can kill an instance of my codemirror? 我正在使用Codemirror v3.16,我试图弄清楚如何杀死我的codemirror实例? Basically codemirror fires when a textarea opens in a modal on my page. 基本上,当textarea在我的页面上的模态中打开时,会触发codemirror。 Closing this modal, I need to kill the instance, else when I reopen the modal, I get two textareas. 关闭这个模态,我需要杀死实例,否则当我重新打开模态时,我得到两个textareas。

Can anyone help? 有人可以帮忙吗?

If the CodeMirror instance was created with CodeMirror.fromTextArea , you can use its toTextArea method to copy the current contents to its "mirrored" text area and remove the instance. 如果CodeMirror实例是使用CodeMirror.fromTextArea创建的, CodeMirror.fromTextArea可以使用其toTextArea方法将当前内容复制到其“镜像”文本区域并删除实例。

Assuming your CM instance has the id of "CMEditor": 假设您的CM实例的ID为“CMEditor”:

CM = document.getElementById('CMEditor');
CM.CodeMirror.toTextArea();

Alternately, you could instantiate the CM instance outside of the modal, and just hide and show it when the modal opens. 或者,您可以在模态之外实例化CM实例,只需在模态打开时隐藏并显示它。

When CodeMirror is removed from the DOM (and you kill all existing references to it that JavaScript might be holding), it'll be garbage collected. 当从DOM中删除CodeMirror(并且您杀死了JavaScript可能持有的所有对它的现有引用)时,它将被垃圾收集。 There is no explicit 'kill' method, you simply stop referring to it. 没有明确的'kill'方法,你只是停止引用它。

Answers above only work for CM on top of textarea which is not always the case. 上面的答案仅适用于textarea顶部的CM,但并非总是如此。 This is better: 这个更好:

cm.setOption("mode", "text/x-csrc");
cm.getWrapperElement().parentNode.removeChild(cm.getWrapperElement());
cm=null;

The version 5.3 or 05/2015 Update 版本5.3或05/2015更新

CM = document.getElementById('CMEditor');

CM.CodeMirror.toTextArea(); // is not working 

CM.toTextArea(); // is making the magic happen

I've added the above code to run when the modal is closed, but it says that --- cmeditor.toTextArea is not a function 我已经添加了上面的代码,当模态关闭时运行,但是它说--- cmeditor.toTextArea不是一个函数

$("body").on("hidden.bs.modal", ".modal", function()
{
    $(this).removeData("bs.modal");

    cmeditor = document.getElementById("email_template_text");
    cmeditor.toTextArea();
});

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

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