在 tinyMCE 中删除 /n/r

remove /n/r in tinyMCE

我有一个场景,我必须读取 html 页面的页面源并将其保存为字符串。我必须在 tinyMCE 中检索此页面源代码。当我将 tinyMCE 的内容设置为这个字符串时,我得到 \r \n。我希望它们显示为换行符而不是字符串。我尝试用 替换字符串,但没有帮助。如果有人可以帮助,请

tinyMCE.init({
    // General options
    mode: "specific_textareas",
    theme: "advanced",
    width: "100%",
    plugins: "pagebreak,paste,fullscreen,visualchars",
    entity_encoding: "raw",
    remove_linebreaks: false,
    init_instance_callback: "customTinyMceInit",
    // Theme options
    theme_advanced_buttons1: "code,|,bold,italic,underline,|,sub,sup,|,charmap,|,fullscreen,|,bullist,numlist,|,pasteword",
    theme_advanced_buttons2: "",
    theme_advanced_buttons3: "",
    theme_advanced_buttons4: "",
    theme_advanced_toolbar_location: "top",
    theme_advanced_toolbar_align: "left",
    theme_advanced_statusbar_location: "bottom",
    valid_elements: "i,sub,sup",
    invalid_elements: "p, script",
    editor_deselector: "mceOthers"

});

function customTinyMceInit(inst) {
    if (window.opener != null && !window.opener.closed) {
        var parent = $(window.opener.document).contents();
        var Id = queryString["Id"];
        var Result = [];


        $.ajax({
            type: "POST",
            url: "/Modules/Management/OnlineTemplateCreation.aspx/GetTemplateByContentId",
            data: "{'Id':"+Id+"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                Result.push(data.d);
                alert(Result[0]);
                tinyMCE.getInstanceById("RichTextBox").setContent(Result[0]);
            }



        });

尝试将响应数据中的“\r \n”字符替换为“< br >”。

function replaceLineBreaks(data) {
     var replacedData = data.replace("\r \n", "<br>");
     return replacedData;
}

然后试试这个:

tinyMCE.getInstanceById("RichTextBox").setContent(replaceLineBreaks(Result[0]));

希望对您有所帮助。

在 init 中应用源格式

tinyMCE.init({
        ...
        apply_source_formatting : true
});

它适用于 tinymce@4.3.13

    tinyMCE.init({
    forced_root_block: false ,
    });

来源:https://www.tinymce.com/docs/configure/content-filtering/#forced_root_block