【问题标题】:JQuery Templates: pass html parameter into a functionJQuery模板:将html参数传递给函数
【发布时间】:2016-05-17 17:38:27
【问题描述】:
我有这个 JQuery 模板:
$.tmpl('<p>${text}</p>', { text: 'cool text' }).appendTo('div');
效果很好。但是现在我想将一个html参数传递给它,但是以下不起作用。
$.tmpl('<p>${text}</p>', { text: '<span>cool text</span>' }).appendTo('div');
有什么建议吗?
【问题讨论】:
标签:
jquery
html
parameters
jquery-templates
【解决方案1】:
您可以使用{{html content}} 构造
$.tmpl('<p>{{html text}}</p>', {
text: '<span>cool text <b>bold</b></span>'
}).appendTo('div');
span {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>
<div></div>
【解决方案2】:
还有一种方法可以做到这一点
var markup = "<span>${text}</span>";
jQuery.template( "template1", markup );
jQuery.tmpl( "template1", { text: "cool text" } ).appendTo( "div" );