我有以下模板
<script id="myTemplate" type="text/x-jquery-tmpl">
<p>${{html Name}}</p>
<p>${Format(Title)}</p>
</script>
和 JavaScript 函数 Format()
function Format(Title){
return "<b>" + Title + "</b>";
}
和实际数据
var data = {
Name: "<h3>Tom</h3>",
Title: "Mr."
};
${{html Name}} 用于显示“Name”中包含的 html 标签。但是,我的函数“Format”返回的 HTML 标记在我的页面上呈现为文本。如何使用 JQuery tmpl 从函数返回 HTML?
最佳答案
引用${...}
doc :
The values rendered by ${} are evaluated as strings, and are HTML encoded. Any embedded markup will therefore be encoded. To insert the unencoded markup in the rendered template, use instead the {{html}} template tag.
因此只需使用 {{html Format(Title)}}
即可修复。
关于Jquery tmpl 从函数返回 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13180031/