简体   繁体   English

tinyMCE 获取编辑器返回 null

[英]tinyMCE get editor returns null

I initialize 2 tinyMCE editor on 2 textarea with different id :我在 2 个具有不同 id 的 textarea 上初始化了 2 tinyMCE 编辑器:

var variable_array = {id:'cName', test:'mon test'};
tinymce.init({
    selector: "#model_editor",
    entity_encoding : "raw",
    encoding: "UTF-8",
    theme: "modern",
    height: "500px",
    width: "100%",
    variables_list : variable_array,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern modelinsert"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image print preview media | forecolor backcolor emoticons",
    toolbar2: "variable_insert | question_insert",
    image_advtab: true,
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
});

tinymce.init({
    selector: "#headerfooter_editor",
    entity_encoding : "raw",
    encoding: "UTF-8",
    theme: "modern",
    height: "500px",
    width: "100%",
    variables_list : variable_array,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern modelinsert"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image print preview media | forecolor backcolor emoticons",
    toolbar2: "variable_insert | question_insert",
    image_advtab: true,
    init_instance_callback : "mceInitInstance",
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
});

Both editors init correctly.两个编辑器都正确初始化。 Then in order to set different content on each, I try to get the editor instance object id :然后为了在每个上设置不同的内容,我尝试获取编辑器实例对象 id :

var editor_id = tinyMCE.get('#headerfooter_editor');
console.log(editor_id);

It returns null :/它返回空:/

I try also to get in the console the result of the callback of the second init :我还尝试在控制台中获取第二个 init 回调的结果:

function mceInitInstance(inst) {

console.log("Editor: " + inst.editorId + " is now initialized.");

And it returns : Editor: undefined is now initialized.它返回:编辑器:未定义现在已初始化。

I want to do the following :我想做以下事情:

tinyMCE.get('#headerfooter_editor').setContent(data.content);

but of course it returns an error : Uncaught TypeError: Cannot read property 'setContent' of null但当然它会返回一个错误:未捕获的类型错误:无法读取 null 的属性“setContent”

I don't understand what's wrong and why I can't get the editor instance id :/我不明白出了什么问题以及为什么我无法获得编辑器实例 ID:/

Your editors should be available using tinymce.get('model_editor') and tinymce.get('headerfooter_editor') .您的编辑器应该可以使用tinymce.get('model_editor')tinymce.get('headerfooter_editor')

Hint : tinymce.editors holds all editor instances that have been initialized.提示tinymce.editors保存所有已初始化的编辑器实例。 You can loop through that array to get them all:您可以遍历该数组以获取全部:

for (var i = 0; i < tinymce.editors.length; i++)
{
    console.log("Editor id:", tinymce.editors[i].id);
}        

Instead of:代替:

tinyMCE.get('#headerfooter_editor').setContent(data.content);

use

tinyMCE.get('headerfooter_editor').setContent(data.content);

remove #删除#

I got the same problem.我遇到了同样的问题。 The error message was:错误消息是:

TypeError: tinymce.get(...) is null

But my error was that I tried to tinymce.get(...) before initiating tinymce editor.但我的错误是我在启动 tinymce 编辑器之前尝试了tinymce.get(...)

tinymce.init({selector: "#mytextarea"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM