简体   繁体   English

动态添加tinymce编辑器

[英]Add a tinymce editor dynamically

I have a page with 2 working tinymce editor when i display it. 当我显示它时,我的页面上有2个可用的tinymce编辑器。 Textarea is dynamicaly added to the page with a user control (page already loaded). Textarea通过用户控件(页面已加载)动态添加到页面中。 The following js/coffescript aim to add a tinymce editor to the new textarea: 以下js / coffescript旨在向新的textarea添加tinymce编辑器:

$(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. 如果我执行alert event.field.find('textarea').attr("id") ,它将返回应在其中添加编辑器的文本字段的正确ID。 However, it does not add tinyMCE to the textarea. 但是,它不会将tinyMCE添加到文本区域。 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. 我还可以看到,当显示警报时,页面上存在textarea,我猜这意味着当脚本尝试添加tinymce时,textarea存在。

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? 我需要更改脚本以成功将tinyMCE添加到新的文本区域吗?

The control on which the tinymce editor will apply needed to render first. 首先需要对tinymce编辑器应用的控件。 The point is it's require the element to be added in DOM and loaded first. 关键是它要求将元素添加到DOM中并首先加载。

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. 并在页面完全加载并将动态控件添加到DOM后调用loadTinyMCEEditor()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM