Changing the value of a Telerik RadEditor with Javascript/jQuery

房东的猫 提交于 2019-12-08 02:25:33

问题


I'm trying to manually clean the HTML of a Telerik RadEditor with Javascript but I can't seem to find the correct place to store the value so that it gets saved on post back.

Here's the JS I have:

$(function () {    
    jQuery.fixHash = function ($html) {      

        // modify $html

        return $html;
    };

    $("#adminEditingArea input[id$='SaveButton']").unbind("click").click(function () {
        $("iframe[id$='_contentIframe']").trigger("save");

        // call .net postback

        return false;
    });

});

var editorSaveEventInit = false;
function InitSaveEvent() {
    if (!editorSaveEventInit) {
        var $EditFrames = $("iframe[id$='_contentIframe']");
        if ($EditFrames && $EditFrames.length > 0) {
            $EditFrames.bind("save", function (e) {
                var $thisFrame = $(this);
                var thisFrameContents = $thisFrame.contents();
                if (thisFrameContents) {
                    var telerikContentIFrame = thisFrameContents.get(0);
                    var $body = $("body", telerikContentIFrame);
                    var html = $.fixHash($body).html();
                    $body.html(html);

                    // also tried storing the modified HTML in the textarea, but it doesn't seem to save:
                    //$thisFrame.prev("textarea").html(encodeURIComponent("<body>" + html + "</body>"));
                }
            });
            editorSaveEventInit = true;
        }
    }
};

$(window).load(function () {
    InitSaveEvent();
});

Is there any way to access the Telerik RadEditor object with JavaScript (using OnClientCommandExecuted()?) so that I can access the .get_html() and .set_html(value) functions? If not, what values do I need to set before posting back?


回答1:


Why don't you use custom content filters.




回答2:


Ah, just discovered Telerik's built-in $find() function: http://www.telerik.com/help/aspnet-ajax/editor_getingreferencetoradeditor.html

Edit: here's the solution I came up with for my InitSaveEvent() function:

var editorSaveEventInit = false;
function InitSaveEvent() {
    if (!editorSaveEventInit) {
        var $EditFrames = $("iframe[id$='_contentIframe']");
        if ($EditFrames && $EditFrames.length > 0) {
            $EditFrames.bind("save", function (e) {
                var $thisFrame = $(this);
                var thisFrameContents = $thisFrame.contents();
                if (thisFrameContents) {
                    var telerikContentIFrame = thisFrameContents.get(0);
                    var $body = $("body", telerikContentIFrame);
                    var html = $.fixHash($body).html();
                    // SOLUTION!
                    var $radeditor = $thisFrame.parents("div.RadEditor.Telerik:eq(0)");
                    var editor = $find($radeditor.attr("id"));
                    editor.set_html(html);
                    // ☺
                }
            });
            editorSaveEventInit = true;
        }
    }
};


来源:https://stackoverflow.com/questions/1234028/changing-the-value-of-a-telerik-radeditor-with-javascript-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!