[英]How to populate tinymce with some content on trigger of javascript event?
我想在javascript中使用selectbox的onchange事件觸發器中的一些內容填充tinymce body。 我嘗試了一些東西,但它對我不起作用。請指導我正確的方向。 這是我的代碼,在textarea上添加了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
});}
});
這是html代碼
<td>
<textarea rows="15" name="MyTinymceContent" cols="90" >MyTestContent</textarea>
現在我想用javascript中的選擇框更改來填充新內容的tinymce。 所以我在更改選擇框時寫下面的代碼片段
function populateMyTinyMCE() {
document.form.MyTinymceContent.value ="MyNewContent";
}
但它並沒有把新內容放在tinymce身上。 我不確定我在這里缺少什么?
您可以在某些事件觸發器上調用以下任何一項:
tinyMCE.get('your_editor').setContent("MyNewContent");
//OR
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';
}
嘗試
tinyMCE.activeEditor.setContent("MyNewContent");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.