mustache.js html模板,mustache.js – 无法使用jQuery get()方法获取外部模板

我想在我的主页中使用jQuery的get()方法包含一个外部模板,但我有一个问题.

Web浏览器控制台中显示以下消息:

TypeError: Invalid template! Template should be a “string” but “undefined” was given as the first argument for mustache#render(template, view, partials) – (mustache.min.js:1:9011)

的index.html

Loading ...

templates.html

{{ name }} wins {{ calc }} points

我-的script.js

// my-script.js

function myTemplate() {

$.get("tpl/templates.html", function(templates) {

var template = $(templates).filter("#tpl-1").html();

var data = {

name: "Guy",

calc: function () {

return 8 + 2;

}

};

var rendered = Mustache.render(template, data);

$('#target').html(rendered);

});

}

然而,它适用于load()方法:

// my-script.js

function myTemplate() {

$("#target").load("tpl/templates.html #tpl-1", function() {

var template = $("#tpl-1").html();

var data = {

name: "Guy",

calc: function() {

return 8 + 2;

}

};

var rendered = Mustache.render(template, data);

$('#target').html(rendered);

});

}

你有想法运行get()方法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值