【问题标题】:htmlentities equivalent in jQuery template pluginjQuery 模板插件中的 htmlentities 等效项
【发布时间】:2011-06-01 23:55:40
【问题描述】:

如何将字符串中的特殊字符转换为 jQuery template plugin 中的等效 htmlentities?我知道 jQuery 中的 $('<div/>').text(str).html() 方法。我正在寻找一种在模板插件中实现它的方法。

【问题讨论】:

    标签: jquery jquery-plugins jquery-templates


    【解决方案1】:

    我认为在将数据传递给模板时,您必须手动使用该技巧。

    $.tmpl (
        yourTemplate,
        {
            yourVar: $('<div/>').text(str).html()
        }
    );
    

    【讨论】:

      【解决方案2】:

      这是你想要的吗?

      js代码:

      var data = { HtmlContent : '<h1>The h1 tag should not display</h>' };
      
      $('#tmplSample').tmpl(data).appendTo('body');
      

      模板:

      <script id="tmplSample" type="text/x-jquery-tmpl">
      <div>
      {{html HtmlContent}}
      </div>
      </script>
      

      这里还有一些示例:

      http://api.jquery.com/template-tag-html/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 1970-01-01
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多