7

有没有办法<head>通过 javascript 访问通过 HTML 加载的 mustache 模板文件的内容?

<link rel="template" href="templates/address.mustache" type="text/html" />

或者

<script src="templates/address_field.mustache" type="text/html" charset="utf-8" id="address_template"></script>

我已经通过 ajax 和<script>正文中的标签成功加载了它们,但是我不确定如何在通过头部作为单独的文件加载时获取文件源。

4

2 回答 2

0

那么您需要使用一个选择器来获取该 html 的内容。

使用 jQuery,它就像这样简单:

var tmpl=$.trim($('#address_template').val()); //trim the white spaces in the template
Mustache.to_html(tmpl,json);
于 2011-07-30T06:19:00.950 回答
0

您需要获取获取模板值外部模板的方法:以下代码仅适用于 html 页面内部:

var tmpl=$.trim($('#address_template').val()); //trim the white spaces in the template

Mustache.to_html(tmpl,json);

您需要以下代码:“ $.get('template.html' ”是您的答案。您应该首先加载模板:

<!DOCTYPE html>

标题

<script>
    $(function() {
        $.getJSON('/data/speakers.json', function(data) {
            //var template = $('#speakers-template').html();

           // console.log(template);
           // var info = Mustache.render(template, data);
           // $('#talktitles').html(info);

$.get('template.html', function(template, textStatus, jqXhr) { var info =Mustache.render($(template).filter('#speakers-template').html(), data); $( '#talktitles').html(信息); });

        });
    });
</script>

于 2014-07-10T09:52:06.603 回答