Tinymce get content

大憨熊 提交于 2019-12-11 03:19:26

问题


I try to get the content of tinymce, like this:

 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.

Thank you


回答1:


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.

<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 :

<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




回答2:


You can get tinyMCE content by calling the the method triggerSave in the following way

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();


来源:https://stackoverflow.com/questions/31475325/tinymce-get-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!