【问题标题】:Reset TinyMCE box重置 TinyMCE 盒子
【发布时间】:2011-08-31 05:54:23
【问题描述】:

我正在使用TinyMCE 编辑器。我想通过我的表单上的一些按钮单击来清除编辑器框中的内容。

你能告诉我怎么做吗?

【问题讨论】:

    标签: javascript jquery asp.net tinymce


    【解决方案1】:

    将指定的内容设置为编辑器实例,这将在设置内容之前清理内容 不同的清理规则选项。

    tinymce.activeEditor.setContent('');
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
    【解决方案2】:

    如果您有兴趣清除编辑器的内容,您可以使用: tinymce.get('#editorId').setContent(''); // 就像其他人建议的那样

    但是,如果您想重置内容和菜单按钮等 - 基本上完全重置编辑器,您可以考虑使用: tinymce.get('#editorId').init();

    【讨论】:

    • 使用反引号`字符突出显示代码部分。
    • 我正在寻找 init() tinymce editor 4.x 的解决方案。我试过这个,但我不得不说它只初始化编辑器的文本区域,菜单(按钮等)和工具栏不会被重新初始化。 :(
    【解决方案3】:
    $('#name_of_your_textarea').val('');
    

    【讨论】:

    • 我不确定您的解决方案如何解决 OP 的问题:您正在重置底层 textarea 的内容,而不是 tinymce 编辑器的内容。
    【解决方案4】:

    使用以下代码作为按钮的 onclick-action 可以轻松完成(无需使用缓慢的 jQuery tinymce 构建):

    // 'content' is tinymce default,
    // but if your textarea got an ID that is the one you need!
    var my_editor_id = 'content';
    
    // set the content empty
    tinymce.get(my_editor_id).setContent(''); 
    

    【讨论】:

    • 为了更清楚起见,my_editor_id 应该真正命名为 my_textarea_id。当我阅读 editor 时,我想到了 tinymce 编辑器 :-)
    • 好吧,两个 id 相同(如果编辑器源 html 元素设置了 id)
    • 这段代码到哪里去了?最好在可用按钮旁边放置一个按钮,在该按钮上运行此代码tinymce.get('content').setContent('');
    • 您可以从主文档中的任何位置调用此代码
    • 在我的情况下,我需要在 setTimeout 匿名函数中调用它,超时为 0。否则它不起作用,并且由于某种原因我在 JS 控制台中遇到了各种错误。只是觉得我应该提一下。
    【解决方案5】:

    来自TinyMCE jQuery Plugin documentation,可以从您链接的页面轻松找到:

    // Will change the contents of an textarea with the ID "someeditor"
    $('#someeditor').html('Some contents...');
    
    // Will change the contents all text areas with the class tinymce
    $('textarea.tinymce').html('Some contents...');
    
    // Gets the contents from a specific editor
    alert($('#someeditor').html());
    

    尝试将其设置为空字符串,可能正是您所需要的。

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多