关于javascript:tinyMCE get编辑器返回null

tinyMCE get editor returns null

我在2个具有不同id的textarea上初始化了2个tinyMCE编辑器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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'}
    ]
});

两个编辑器均正确初始化。
然后,为了在每个对象上设置不同的内容,我尝试获取编辑器实例对象id:

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

它返回null:/

我也尝试在控制台中获取第二个init的回调结果:

1
2
3
function mceInitInstance(inst) {

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

它返回:编辑器:未定义现在已初始化。

我要执行以下操作:

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

但它当然会返回错误:未捕获的TypeError:无法读取null的属性'setContent'

我不明白这是什么问题,为什么我无法获得编辑器实例ID:/


使用tinymce.get('model_editor')tinymce.get('headerfooter_editor')可以使用您的编辑器。

提示:tinymce.editors保存所有已初始化的编辑器实例。
您可以遍历该数组以获取所有内容:

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


而不是:

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

使用

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

删除#


我遇到了同样的问题。
错误消息为:

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

但是我的错误是我在启动tinymce编辑器之前尝试了tinymce.get(...)

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