【问题标题】:initialize tinymce with content用内容初始化tinymce
【发布时间】:2014-11-10 08:31:27
【问题描述】:

我有一个包含一个 tinymce4 编辑器实例的页面。我想以编程方式用一些内容初始化这个编辑器。我知道我必须打电话:tinymce.get('editor').setContent('my content');

但是,当 tinymce 初始化时,我很难做到这一点。已经问过这个问题:initialize tinyMCE with default content 但当时给出的答案不起作用,至少对于 tinymce4 而言。

这是我尝试过的:

第一次尝试:

    tinymce.init({
      mode: "textareas",
      ...
      setup: function (editor) {
         ...
         editor.setContent('my content');
      }
   });

-> Uncaught TypeError: Cannot read property 'body' of undefined

第二次尝试:

    tinymce.init({
      mode: "textareas",
      ...
    };
    tinymce.get('editor').setContent('my content');

-> 未捕获的类型错误:无法读取 null 的属性“setContent” (但是,如果我在已加载包含 tinymce 编辑器的页面时执行此操作,则它可以工作)。

第三次尝试(SO 12083361 答案):

$(document).ready(function(){
    tinymce.get('editor').setContent('my content');
});

-> 未捕获的类型错误:无法读取 null 的属性“setContent”

tinymce.activeeditor.setContent('my content'); 也会失败。

我应该将tinymce.get('editor').setContent('my content');放在我的代码中的哪个位置以使其正常工作?

【问题讨论】:

    标签: javascript tinymce tinymce-4


    【解决方案1】:

    对于版本 4,您应该这样做:

    tinymce.init({selector:'textarea'});
    tinymce.activeEditor.setContent('custom');
    

    这是fiddle

    【讨论】:

    • 这很好......但是当页面上有多个需要单独定位的编辑器时缺乏:-(
    • 如果页面上有多个 RTE 怎么办?有没有其他解决办法...
    • @DamienSawyer 我收到此错误tinymce editor error cannot read property doc is undefined 你能建议我吗?代码var body = tinymce activeEditor.dom.doc.body
    【解决方案2】:

    我使用了 init_instance_callback,它在编辑器准备好设置内容时触发。

    var options = { ... };
    options.init_instance_callback = function (editor) {
        editor.setContent(initialContent);
    };
    $("some-selector").tinymce(options);

    【讨论】:

    • 我收到此错误tinymce editor error cannot read property doc is undefined 你能建议我吗?代码var body = tinymce activeEditor.dom.doc.body
    猜你喜欢
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多