根据get() API,您传递给该调用的字符串必须是编辑器元素的ID,而不是类。
https://www.tinymce.com/docs/api/class/tinymce/#get
所以如果你想根据 ID 定位编辑器,你需要这样的东西:
<textarea class='tiny-mce' id='editor1'></textarea>
<textarea class='tiny-mce' id='editor2'></textarea>
...在你的 JavaScript 中是这样的...
tinymce.get('editor2').setContent('...content here...');
如果页面上只有一个编辑器,也可以使用
tinymce.activeEditor.setContent('...content here...');
编辑:这取决于您要在编辑器的生命周期中何时调用get() 或activeEditor 方法。
在 TinyMCE 完全初始化之前,这些不会返回任何内容。如果你有这样的代码:
<form method="post" action="dump.php">
<textarea class='tiny-mce' id='editor1'></textarea>
<textarea class='tiny-mce' id='editor2'></textarea>
</form>
<script>
tinymce.get('editor2').setContent("content here");
</script>
这通常会失败,因为您不知道在 JavaScript 运行时 TinyMCE 是否已完成对 textareas 的初始化。请看这个小提琴:
http://fiddle.tinymce.com/gufaab/1
将内容加载到编辑器中的最佳方式是使用编辑器自己的“init”回调并将您的代码放入其中。例如:
tinymce.init({
selector: "textarea",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
setup: function (editor) {
editor.on('init', function () {
this.setContent('The init function knows on which editor its called - this is for ' + editor.id);
});
}
});
...请注意,每个编辑器(如果有多个)都会调用 init,如果需要,您可以访问 DOM 中的编辑器对象。