如何在JavaScript事件的触发器上添加一些内容的tinymce?

问题描述:

我想在javascript中的selectbox的onchange事件的触发器上添加一些内容来填充tinymce主体。我尝试了一些东西,但它确实不适合我,请引导我向正确的方向发展。 这里是我的代码,在附加textarea的如何在JavaScript事件的触发器上添加一些内容的tinymce?

$(function() { 
appendTinyMCE(); 
function appendTinyMCE(){ 
    tinyMCE.init({ 

     // General options 
     mode : "textareas", 
     theme : "advanced", 
     plugins : "preview", 
     // Theme options 
     theme_advanced_buttons1 : "forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,|,formatselect,fontselect,fontsizeselect,sub,sup,|,bold,italic,underline,strikethrough", 
     theme_advanced_buttons2 : "", 
     theme_advanced_toolbar_location : "top", 
     theme_advanced_toolbar_align : "left", 
     theme_advanced_statusbar_location : "bottom", 
     theme_advanced_resizing : true 

});} 

})TinyMCE的;

下面是HTML代码

<td> 
     <textarea rows="15" name="MyTinymceContent" cols="90" >MyTestContent</textarea> 

现在我想填充新的内容TinyMCE的,在JavaScript的选择框的变化。所以我写一篇关于选择框

function populateMyTinyMCE() { 
    document.form.MyTinymceContent.value ="MyNewContent"; 
} 

的变化 下面的代码片段,但它并没有放在TinyMCE的身体新的内容。我不确定我在这里错过了什么?

+0

+1准确和详尽的问题 – Thariama 2012-07-17 11:12:25

您可以拨打下面的一些事件触发的任何:

tinyMCE.get('your_editor').setContent("MyNewContent"); 
//OR 
tinyMCE.activeEditor.setContent('MyNewContent'); 

参见:Here for More

+0

嘿的Sudhir,tinyMCE.activeEditor.setContent( 'MyNewContent')为我工作。出于好奇,如果我想使用tinyMCE.get('your_editor').setContent(“MyNewContent”),我应该放置什么而不是'your_editor',因为我没有在tinymce中看到任何id属性。 – 2012-07-17 11:48:02

+0

'your_editor'将是您的textarea的编号... – 2012-07-17 11:52:28

+0

如果您的textarea没有id“内容”将被用作默认值 – Thariama 2012-07-17 12:25:34

尝试

tinyMCE.activeEditor.setContent("MyNewContent"); 

TinyMCE的不等于textarea的。在编辑器初始化时,创建了一个可以自定义的iframe。此iframe保存编辑器内容,并不时写回编辑器源html元素(在您的情况下为textarea)。要设置编辑器内容,您需要使用tinymce setContent函数。我会告诉替代太:

function populateMyTinyMCE() { 

    var editor = tinymce.editors[0]; 

    // other ways to get the editor instance 
    // editor = tinymce.get('my editor id'); 
    // editor = tinymce.activeEditor; 

    editor.setContent('my new content'); 

    // alternate way of setting the content 
    // editor.getBody().innerHTML = 'my new content'; 
}