javascript Tinymce 获取内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31475325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tinymce get content
提问by InfinityGoesAround
I try to get the content of tinymce, like this:
我尝试获取tinymce的内容,如下所示:
var hallo = tinyMCE.activeEditor.getContent();
alert(hallo);
but every time I get this message:
但每次我收到这条消息时:
Uncaught TypeError: Cannot read property 'getContent' of null
I am using tinymce 4.
我正在使用 tinymce 4。
Thank you
谢谢
回答by Patel
Cannot read property 'getContent' of null
often means that TinyMCE
is unable to find your textbox which means there is something wrong in the reference to textarea's class
.
Cannot read property 'getContent' of null
通常意味着TinyMCE
无法找到您的文本框,这意味着对 textarea 的class
.
<form method="post" action="somepage">
<textarea id="myTextArea" class="mceEditor">I should buy a boat. </textarea>
</form>
<button onclick="content()">Get content</button>
Take note of mceEditor
class which we will now inform the TinyMCE
editor about :
记下mceEditor
我们现在将通知TinyMCE
编辑的课程:
<script type="text/javascript">
tinyMCE.init({
mode : "specific_textareas",
editor_selector : "mceEditor" //<<<----
});
</script>
And now simply get the contents of that textbox on the button click.
现在只需单击按钮即可获取该文本框的内容。
function content() {
alert(tinyMCE.get('myTextArea').getContent());
}
Here is working DEMO
这是工作演示
回答by Shahbaz
You can get tinyMCE content by calling the the method triggerSavein the following way
您可以通过以下方式调用triggerSave方法来获取tinyMCE内容
tinyMCE.triggerSave();
after declaring this method you can get the contents by selector for example:-
声明此方法后,您可以通过选择器获取内容,例如:-
var contents = $("#myTextArea").val();
or
或者
var contents = tinyMCE.get('myTextArea').getContent();