Add a tinymce editor dynamically

南楼画角 提交于 2019-12-13 19:15:15

问题


I have a page with 2 working tinymce editor when i display it. Textarea is dynamicaly added to the page with a user control (page already loaded). The following js/coffescript aim to add a tinymce editor to the new textarea:

$(document).on 'nested:fieldAdded', (event) ->
  tinyMCE.execCommand("mceAddControl", false, event.field.find('textarea').attr("id"))

If i do an alert event.field.find('textarea').attr("id"), it returns the correct ID of the textfield where the editor should be added. However, it does not add tinyMCE to the textarea. I can also see that when the alert is displayed, the textarea exist on the page, I guess it means it exist when the script try to add tinymce.

At the top of my body i have the following script :

<script type="text/javascript">
//<![CDATA[
tinyMCE.init({"selector":"textarea.tinymce","theme_advanced_toolbar_location":"top","theme_advanced_toolbar_align":"left","theme_advanced_statusbar_location":"bottom","theme_advanced_buttons3_add":"tablecontrols,fullscreen","plugins":"paste,table,fullscreen","dialog_type":"modal","content_css":"/assets/application.css"});
//]]>
</script>

What do I need to change to my script to succesfully add tinyMCE to the new textarea?


回答1:


The control on which the tinymce editor will apply needed to render first. The point is it's require the element to be added in DOM and loaded first.

May be you can try to add some time delay to your script for adding/applying the editor.

Try this (may this will help you)

Make a function

function loadTinyMCEEditor() {
  tinyMCE.init({
  });
}

and call loadTinyMCEEditor() after page fully loaded and your dynamic control added to DOM.



来源:https://stackoverflow.com/questions/19157574/add-a-tinymce-editor-dynamically

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