删除TinyMCE控件并重新添加

时间:2012-04-10 20:07:09

标签: javascript html tinymce state

我有一个永远不会重新加载页面的js应用程序,因此在导航时我需要完全删除TinyMCE控件,然后我想在导航到需要它的区域时重新初始化。我尝试了这个问题的接受答案,但似乎什么也没做。

How do i remove tinyMCE and then re-add it?

 tinymce.EditorManager.execCommand('mceRemoveControl',true, editor_id);

和我的具体意见:

  //if I throw an alert here, it does get called, so I know it's not null
  if (tinyMCE.getInstanceById("main-text"))
            tinyMCE.EditorManager.execCommand('mceRemoveControl', true, "main-text");

我也试过

  tinyMCE.remove( tinyMCE.getInstanceById("main-text"));
  // AND
  tinyMCE.remove( "main-text");

我知道当我在条件中发出警报时,该语句会被执行...我知道这是正确的ID-- API中是否还有其他隐藏的东西?这不是jQuery版本。编辑器在删除尝试后仍然存在,如果我通过导航回到具有该表单的状态重新启动它,我甚至会获得具有相同ID的新编辑器。

编辑:下面的解决方案在当前版本3.5b3中不起作用,仅在3.4.9中。有一个“未定义”的错误

以防万一,这是init之后DOM的相关部分。

 <textarea id="main-text" style="display: none;" aria-hidden="true"></textarea>
<span id="main-text_parent" class="mceEditor defaultSkin" role="application" aria-labelledby="main-text_voice" style="display: inherit;">
<span id="main-text_voice" class="mceVoiceLabel" style="display:none;">Rich Text Area</span>
<table id="main-text_tbl" class="mceLayout" cellspacing="0" cellpadding="0" role="presentation" style="width: 100%; height: 400px;">
<tbody>
<tr class="mceFirst" role="presentation">
<td class="mceToolbar mceLeft mceFirst mceLast" role="presentation">
<div id="main-text_toolbargroup" aria-labelledby="main-text_toolbargroup_voice" role="group" tabindex="-1">
<span role="application">
</div>
<a onfocus="tinyMCE.getInstanceById('main-text').focus();" title="Jump to tool buttons - Alt+Q, Jump to editor - Alt-Z, Jump to element path - Alt-X" accesskey="z" href="#"></a>
</td>
</tr>
<tr>
<tr class="mceLast">
</tbody>
</table>
</span>

2 个答案:

答案 0 :(得分:21)

任何遇到这篇文章的人。现在 tinymce 4.X 支持删除编辑器或编辑器表单页面,只需调用remove函数。

http://www.tinymce.com/wiki.php/api4:method.tinymce.remove

// Remove all editors bound to divs
tinymce.remove('div');

// Remove all editors bound to textareas
tinymce.remove('textarea');

// Remove all editors
tinymce.remove();

// Remove specific instance by id
tinymce.remove('#id');

答案 1 :(得分:5)

我遇到过这几次。为了解决这个问题,我确保tinyMCe控件没有焦点(在某些浏览器中导致一些错误),删除tinyMCE控件,并删除控件所关联的文本区域。

以下是相关代码:

if (typeof tinyMCE != 'undefined') {
    tinyMCE.execCommand('mceFocus', false, 'main-text');
    tinyMCE.execCommand('mceRemoveControl', false, 'main-text');
    var container = document.getElementById('main-text-parent');
    container.removeChild(document.getElementById('main-text'));  
    //i normally just do $("#main-text").remove(); but you specified not using jquery, so this should, in theory, remove it, if main-text-parent is replaced with the parent container of your main-text.
}
相关问题