【问题标题】:RadEditor keeps removing <!doctype> tag when switching from HTML to Design, how do I stop this从 HTML 切换到 Design 时,RadEditor 不断删除 <!doctype> 标签,我该如何阻止它
【发布时间】:2014-10-15 01:03:59
【问题描述】:

我在我的项目中使用 RadEditor,但是当我在 HTML 编辑区域的顶部插入然后将视图切换到设计并返回时。标签被移除。我想在不修改太多现有过滤器的情况下阻止 RadEditor 执行此操作。

您可以在以下链接中尝试一下

http://demos.telerik.com/aspnet-ajax/editor/examples/contentfilters/defaultcs.aspx

【问题讨论】:

    标签: javascript c# html telerik radeditor


    【解决方案1】:

    问题在于您使用的是 DIV 模式而不是 iFrame 模式。如果您想使用 DIV 模式,问题是该页面中已经存在 doctype、head、footer 等。一个页面中不能存在两次,浏览器会将其从您的页面中删除。

    您可以使用 iFrame 模式或以下解决方案。

    我使用的解决方案是用我的自定义标签替换该标签,当我从设计模式进入编辑模式并且将其保存到数据库时,我还将我的自定义标签替换为普通标签。例如:

    下面的代码是我在我的项目中使用的一个例子(还没有包含文档类型):

    // Add the custom filter to the editor
    function OnClientLoad(editor, args) {
        editor.get_filtersManager().add(new MyFilter());
    }
    
    // Replace the tags HTML does not expect twice
    var tags = ["html", "body", "head", "title", "form", "textarea"];
    
    MyFilter = function () {
        MyFilter.initializeBase(this);
        this.set_isDom(false);
        this.set_enabled(true);
        this.set_name("RadEditor filter");
        this.set_description("RadEditor filter description");
    }
    
    MyFilter.prototype =
    {
        getHtmlContent: function (content) {
            var newContent = content;
    
            for (var i = 0; i < tags.length; i++) {
                var tag = tags[i];
                newContent = newContent.replace(new RegExp("<j" + tag, "gi"), "<" + tag);
                newContent = newContent.replace(new RegExp("<" + "/j" + tag + "", "gi"), "<" + "/" + tag + "");
            }
    
            return newContent;
        },
        getDesignContent: function (content) {
            var newContent = content;
            for (var i = 0; i < tags.length; i++) {
                var tag = tags[i];
                newContent = newContent.replace(new RegExp("<" + tag, "gi"), "<j" + tag);
                newContent = newContent.replace(new RegExp("<" + "/" + tag + "", "gi"), "<" + "/j" + tag + "");
            }
            return newContent;
        }
    }
    MyFilter.registerClass('MyFilter', Telerik.Web.UI.Editor.Filter);
    

    并且从编辑器中获取 HTML 也替换新标签:

    function GetEditorHtmlCall() {
        var html = $find("RadEditor1").get_html(true);
    
        for (var i = 0; i < tags.length; i++) {
            var tag = tags[i];
            html = html.replace(new RegExp("<" + tag, "gi"), "<j" + tag);
            html = html.replace(new RegExp("<" + "/" + tag + "", "gi"), "<" + "/j" + tag + "");
        }
    
        return html;
    }
    

    上面的代码将替换下面的示例。在设计模式下你有一个 jhtml 标签,在 html 模式下你有你的 html 标签。当你保存它时,你将 jhtml 标记转换回 html,所以它在数据库中很好:

    <html></html> to <jhtml></jhtml>
    

    希望这能为您提供您正在寻找的解决方案。

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多