下划线/ jQuery模板注释问题

时间:2014-06-25 05:35:59

标签: javascript jquery templates underscore.js

我有_.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的方式吗?我以为它应该忽略评论。

1 个答案:

答案 0 :(得分:0)

我不知道这是否对您有用,但我以这种方式使用相同的工具:

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

<script type="text/html" id="desktop" src="partials/desktop.html"></script>     <script src="js/app.min.js"></script>

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

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