【问题标题】:TinyMCE - Set Editor ContentTinyMCE - 设置编辑器内容
【发布时间】:2012-04-06 13:42:00
【问题描述】:

我正在为我的 TinyMCE 编辑器构建自动保存功能。

使用 jQuery,我已将编辑器的内容保存到我的数据库中,现在想将内容重新加载到编辑器中。我在使用来自数据库的 HTML 中的引号 (") 时遇到问题。

我的代码:

var content = "<%=content%>" // Classic ASP variable containing HTML from DB
tinyMCE.activeEditor.setContent(content);

示例输出:

var content = "<p>Oh yes, from Churchill, the <em><span style="text-decoration: underline;"><strong>dog</strong></span></em>.</p>"
tinyMCE.activeEditor.setContent(content);

在变量“content”中,样式标签中有双引号导致JS错误。我该如何解决这个问题?我是用单引号替换引号还是使用转义或编码功能?请帮忙。

【问题讨论】:

    标签: javascript jquery tinymce


    【解决方案1】:

    VBScript 具有 Javascript 等转义功能(如果您在经典 asp 中使用 JScript 作为默认语言,也可以使用)
    查看Escape 和UnEscape。
    以下是将服务器端变量附加到客户端 js 块的有效方法。

    // escape on server-side, unescape with js
    var content = unescape("<%= escape(content) %>");
    tinyMCE.activeEditor.setContent(content);
    

    示例输出:

    var content = unescape("%3Cp%3EOh%20yes%2C%20from%20Churchill%2C%20the%20%3Cem%3E%3Cspan%20style%3D%22text-decoration%3A%20underline%3B%22%3E%3Cstrong%3Edog%3C/strong%3E%3C/span%3E%3C/em%3E.%3C/p%3E");
    tinyMCE.activeEditor.setContent(content);
    

    【讨论】:

      【解决方案2】:

      如果只是 HTML(没有 javascript),用单引号替换双引号可能是最快的。

      【讨论】:

      • 谢谢比弗勒斯。这是我的问题,如果我的用户在他们正在编写的编辑器文章中使用了双引号,然后我用单引号替换它们,我的用户看起来会有点奇怪。这整件事让我感到困惑:)
      【解决方案3】:
      str = "<span style="color:black">Text "string"</span>";
      tag_pattern = /\<.*\>/m;
      matches = str.match(tag_pattern);
      //copy matches array and replace quotes, then use matches array to replace into original string
      

      【讨论】:

        【解决方案4】:

        除了 BeaverusIVs 的第一个答案,我还必须添加这一行

        str = "<span style="color:black">Text "string"</span>";
        

        应该是这样的

        str = '<span style="color:black">Text "string"</span>';
        

        否则字符串无效并抛出错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多