有没有办法访问通过 HTML 加载的 mustache 模板文件的内容 <head>
通过 javascript?
<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>
加载了它们标记在主体中,但我不确定如何通过头部作为单独的文件加载时获取文件源。
最佳答案
您需要获取方法来捕获外部模板的模板值:以下代码仅在 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>
关于javascript - 访问加载在 html head 标签中的 mustache 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722521/