在jQuery UI对话框上初始化TinyMce

Initiating TinyMce on jQuery UI dialog

本文关键字:初始化 TinyMce 对话框 jQuery UI      更新时间:2023-09-26

#open_dialog上的点击事件触发一个jQuery UI对话框,并向/ajax/request/url/发出ajax请求

我想在一个从ajax请求发回的文本区域上启动Tinymce。

用下面的代码我得到日志消息"ajax完成!"每次我点击#open_dialog(和ajax请求完成),但Tinymce只加载第一次对话框打开。怎么会?我如何在每次加载对话框时启动tinymce ?

$('#open_dialog').click(function() {
    $.when($.ajax("/ajax/request/url/")).done(function() {
        console.log("ajax done!");  
        tinymce.init({selector:"textarea",
            toolbar: "undo redo cut copy paste | bold italic underline | bullist numlist | table | styleselect | removeformat ",
            plugins: "paste, table",
            paste_word_valid_elements: "b,strong,i,em,h1,h2,table,tr,td,th",
            menubar: false,
            statusbar: true,
            resize: "both"
        });
    });
});

最终通过删除旧的DOM和旧的编辑器解决了这个问题。下面的ee_body是textarea的DOM id。

$(document).ready(function() {
    tinymce.init({
        mode:"none",
        toolbar: "undo redo cut copy paste | bold italic underline | bullist numlist | table | styleselect | removeformat ",
        plugins: "paste, table",
        paste_word_valid_elements: "b,strong,i,em,h1,h2,table,tr,td,th",
        menubar: false,
        statusbar: true,
        resize: "both"
    });
});
$('#open_dialog').click(function(){
    //Remove old editors and DOM-elements 
    tinymce.EditorManager.execCommand("mceRemoveEditor", false, "ee_body");
    $('#ee_body').remove();
    // When ajax request to new email is done, load Tinymce 
    $.when($.ajax("/ajax/request/url/")).done(function() {
        tinyMCE.execCommand("mceAddEditor", true, "ee_body");   
    }); 
});