访问加载在html head标签中的mustache模板
Access a mustache template that was loaded in the html head tag?
是否有方法访问通过HTML <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>
标签,但我不确定如何获得文件源时,通过头部作为一个单独的文件加载。
你需要get方法来捕捉模板值外部模板:下面的代码只在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>
你需要使用一个选择器来获取html的内容。
使用jQuery,它就像这样简单:
var tmpl=$.trim($('#address_template').val()); //trim the white spaces in the template
Mustache.to_html(tmpl,json);
相关文章:
- 如何配置browserfy与Karma使用转换Stringify为Mustache模板
- 数据绑定到Mustache模板?(最好干净简单)
- 使用Webpack加载Mustache模板
- 如何为具有嵌套对象的 JSON 数据创建 Mustache 模板
- 重用另一个模板中的 Mustache 模板,如果两者都在一个 html 文件中定义
- 属性名称未知的Mustache模板
- 在Mustache模板中嵌入一个Twitter共享按钮(包含JS和所有内容)
- 在视图之间传递Mustache模板数据
- Scala:如何在Finatra中的Mustache模板中包含JS脚本
- 如何获取Mustache模板的所有可用变量
- 如何使用Mustache模板将选项添加到下拉列表中
- 使用jQuerygetJSON将JSON数据的嵌套对象返回到Mustache模板
- 使用对象数组的Mustache模板
- 如何在Mustache模板中处理IF语句
- 在mustache模板中对点击事件执行函数
- 如何在js中从mustache模板中读取动态数据id值
- 在knockout.js中使用mustache模板
- 如何在Mustache模板中使用jQuery内置函数
- Mustache模板-使用数组值作为对象键
- 组合多个mustache模板,然后渲染HTML