make readonly/disable tinymce textarea

Ahm*_*nas 47 html javascript jquery wysiwyg tinymce

我需要在运行时禁用或只读一个tinymce textarea.

Tha*_*ama 59

使用配置参数readonly

tinyMCE.init({
        ...
        theme : "advanced",
        readonly : 1
});
Run Code Online (Sandbox Code Playgroud)

这是演示链接.

更新:阻止用户编辑编辑器内容的方法是将编辑器iframe体的contenteditable属性设置为false:

tinymce.activeEditor.getBody().setAttribute('contenteditable', false);
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,请查看以下SO问题:http://stackoverflow.com/questions/8946391/set-tinymce-editor-param-after-initialized (3认同)
  • 谢谢,但我需要在运行时这样做,而不是在启动时 (2认同)

gra*_*sek 45

从版本4.3.x开始,您可以使用以下代码进行只读模式

tinymce.activeEditor.setMode('readonly');
Run Code Online (Sandbox Code Playgroud)

对于设计模式:

tinymce.activeEditor.setMode('design'); 
Run Code Online (Sandbox Code Playgroud)

  • 这似乎也是4.3.x中唯一可行的解​​决方案,因为其他建议根本不起作用. (2认同)
  • 问题是它不仅禁用编辑,还禁用所有插件,包括预览和查看源代码 (2认同)
  • 伟大的!你也可以使用`tinyMCE.get('editor_ID').setMode('readonly');` (2认同)

小智 23

如果您只有一个编辑器,则可以:

tinymce.activeEditor.getBody().setAttribute('contenteditable', false);
Run Code Online (Sandbox Code Playgroud)

如果您有多个编辑器,则必须通过textarea的id选择它们:

tinyMCE.get('textarea_id').getBody().setAttribute('contenteditable', false);
Run Code Online (Sandbox Code Playgroud)


jos*_*ell 11

Thariama的解决方案将页面上的所有TinyMCE textareas设置为只读.

我发现的最佳解决方案是由Magnar Myrtveit 发布的,它将字段设置为readonly,具有readonly属性.这是代码:

tinyMCE.init({
    ...
    setup: function(ed) {
        if ($('#'+ed.id).prop('readonly')) {
            ed.settings.readonly = true;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)