【问题标题】:Underscore/jQuery template comments issue下划线/jQuery 模板注释问题
【发布时间】:2014-06-25 05:35:59
【问题描述】:

我对 _.template(或 jQuery(或我对这两者的知识))有一个奇怪的问题。我使用 Underscore 渲染模板和 jQuery 将元素附加到 DOM(以及许多其他东西)。 像这样:

var $section = $(this.tmpl.section(data));

一切都很好,但是当我在模板 html 中添加 html 注释时,比如

<script id="section-template" type="text/template">
    <!-- Section panel -->
    <div class="panel section-panel panel-info section-<%= type %>" data-type="<%= type %>" data-layout="<%= layout %>">
        <div class="panel-heading section-handle">
            <h3 class="panel-title"><%= title %></h3>
        </div>
        <div class="panel-body"></div>
    </div>
</script>

我收到一个对象数组。我可能在这里遗漏了一些基本的东西,但这是 jQuery 在输入这个 html 时应该工作的方式吗?我认为它应该忽略 cmets。

【问题讨论】:

  • 尝试使用
  • 在脚本中// 触发 cmets
  • @ejay_francisco 你有关于非 JavaScript 脚本的参考吗?
  • &lt;%-- Section panel --%&gt; 不起作用,// cmets 起作用,但那是下划线 cmets,它们没有显示在 html 代码中,我想在前面放一些漂亮的 cmets - 结束)但无论如何,感谢您的建议。可能不添加任何额外逻辑的唯一方法是将 cmets 包含在 div 中,但这并不是我想要的。
  • 这与下划线无关,这只是 jQuery 对字符串中有多少标签感到困惑:jsfiddle.net/ambiguous/E8Caq 你为什么要在模板中添加评论?

标签: javascript jquery templates underscore.js


【解决方案1】:

我不知道这是否适合你,但我以这种方式使用相同的工具:

  • 在我的首页(通常是 index.html 页面)中,我引用了我的模板

&lt;script type="text/html" id="desktop" src="partials/desktop.html"&gt;&lt;/script&gt; &lt;script src="js/app.min.js"&gt;&lt;/script&gt;

  • 然后在我的主脚本(app.min.js)中,我以这种方式加载模板:

    $.get($("#desktop").attr("src"), function (res) { var template = res; $("body").html(_.template(template)); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多