javascript 如何在启动时删除所有 tinymce 实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3692650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove all tinymce instances at startup?
提问by Le Havre
I'm dynamically creating and destroying textareas for this purpose. However, when I create a textarea and then an instance of it in tinymce--then come back to the page again, it doesn't work. I've found that the solution is to simply remove any existing instance of that same name, but I was wondering if it's possible to just do it at startup.
为此,我正在动态创建和销毁文本区域。但是,当我创建一个 textarea 然后在 tinymce 中创建它的一个实例时-然后再次返回该页面时,它不起作用。我发现解决方案是简单地删除同名的任何现有实例,但我想知道是否可以在启动时执行此操作。
Thanks in advance!
提前致谢!
回答by Thariama
You have to make sure that the textareas or other elements for which you create a tiny instance need to have different ids.
您必须确保为其创建小实例的 textareas 或其他元素需要具有不同的 id。
To remove all tinymce instance you may use (tinymce3):
要删除您可以使用的所有 tinymce 实例 (tinymce3):
for (var i = tinymce.editors.length - 1 ; i > -1 ; i--) {
var ed_id = tinymce.editors[i].id;
tinyMCE.execCommand("mceRemoveControl", true, ed_id);
}
For tinymce4 use:
对于 tinymce4 使用:
for (var i = tinymce.editors.length - 1 ; i > -1 ; i--) {
var ed_id = tinymce.editors[i].id;
tinyMCE.execCommand("mceRemoveEditor", true, ed_id);
}
Make sure to shut down instances the right way when you try to reinitialize a tinymce instance. Otherwise your editor window could stay white or it is not editable at all.
当您尝试重新初始化 tinymce 实例时,请确保以正确的方式关闭实例。否则您的编辑器窗口可能会保持白色或根本不可编辑。
回答by phlegx
For me works this solution:
对我来说,这个解决方案有效:
tinymce.editors = [];
So, you can clear the editors array first and reinitialize the editor with init:
因此,您可以先清除编辑器数组,然后使用 init 重新初始化编辑器:
tinymce.editors = [];
tinymce.init({
selector: 'textarea.tinymce',
...
});
回答by malisokan
My colleague Beni discovered a smart solution to remove all existing TinyMCEs:
我的同事 Beni 发现了一种删除所有现有 TinyMCE 的智能解决方案:
if(typeof(tinyMCE) !== 'undefined') {
var length = tinyMCE.editors.length;
for (var i=length; i>0; i--) {
tinyMCE.editors[i-1].remove();
};
}
回答by jzimmerman2011
This is what I'm using and appears to work fine:
这是我正在使用的并且似乎工作正常:
while (tinymce.editors.length > 0) {
tinymce.remove(tinymce.editors[0]);
}
回答by Nitin Garg
You can use these lines in onload function of javascript or on saving form of previous instance
您可以在 javascript 的 onload 函数或先前实例的保存表单中使用这些行
if (tinyMCE.getInstanceById(id) != null)
{
tinyMCE.execCommand('mceRemoveControl', true, id);
}
where id is the id of textarea or input on which the tinyMce is present
其中 id 是存在 tinyMce 的 textarea 或 input 的 id

