javascript - 如何在 javascript 事件触发时用一些内容填充tinymce?

标签 javascript tinymce wysiwyg

我想在javascript中选择框的onchange事件触发时用一些内容填充tinymce主体。我尝试了一些东西,但确实如此 不适合我。请引导我正确的方向。 这是我的代码,将tinymce附加到textarea

$(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 不等于文本区域。编辑器初始化时,会创建一个可编辑内容的 iframe。该 iframe 保存编辑器内容,并时不时地写回编辑器源 html 元素(在您的情况下为文本区域)。要设置编辑器内容,您需要使用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';
 }

关于javascript - 如何在 javascript 事件触发时用一些内容填充tinymce?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11521063/

相关文章:

javascript - 第一个动态添加的 TinyMCE 编辑器显示,其他的没有

javascript - 如何将对象传递给函数并将结果返回到原始函数

javascript - 加载网页的一部分

javafx - TinyMCE从记事本(剪贴板)复制粘贴到java 1.8上的javafx WebView

javascript - Angular UI Tinymce 从 ng-model 中删除内容,并且在不使用 `&lt;textarea&gt;` 时,2 向绑定(bind)不起作用

tinymce 切换问题

javascript - 如何检查用户是否已通过身份验证以访问特定路径(MEAN STACK)

javascript - 使用 Angular.fromJson 数据数组始终处理为字符串而不是数组

javascript - 拖放后 TinyMCE 编辑器内容丢失

twitter-bootstrap - Bootstrap 所见即所得 : how to get the formatted text on the server side?