tinyMCE 获取编辑器返回 null
2015-11-19
15698
我在 2 个具有不同 id 的文本区域上初始化了 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'}
]
});
两个编辑器均正确初始化。 然后,为了在每个编辑器上设置不同的内容,我尝试获取编辑器实例对象 id:
var editor_id = tinyMCE.get('#headerfooter_editor');
console.log(editor_id);
它返回 null:/
我还尝试在控制台中获取第二个 init 的回调结果:
function mceInitInstance(inst) {
console.log("Editor: " + inst.editorId + " is now initialized.");
它返回:编辑器:未定义现在已初始化。
我想执行以下操作:
tinyMCE.get('#headerfooter_editor').setContent(data.content);
但它当然会返回错误:未捕获的 TypeError:无法读取 null 的属性“setContent”
我不明白出了什么问题以及为什么我无法获取编辑器实例 id:/
3个回答
您的编辑器应该可以使用
tinymce.get('model_editor')
和
tinymce.get('headerfooter_editor')
。
提示
:
tinymce.editors
保存已初始化的所有编辑器实例。
您可以循环遍历该数组以获取所有实例:
for (var i = 0; i < tinymce.editors.length; i++)
{
console.log("Editor id:", tinymce.editors[i].id);
}
Thariama
2015-11-20
不要使用:
tinyMCE.get('#headerfooter_editor').setContent(data.content);
使用
tinyMCE.get('headerfooter_editor').setContent(data.content);
删除
#
Dominic Tan
2016-07-19
我遇到了同样的问题。 错误消息是:
TypeError: tinymce.get(...) is null
但我的错误是我在启动 tinymce 编辑器之前尝试了
tinymce.get(...)
。
tinymce.init({selector: "#mytextarea"})
rnviii
2020-08-31