Jquery tmpl 从函数返回 HTML

标签 jquery jquery-templates

我有以下模板

<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/

相关文章:

javascript - 获取 jquery tmpl 中的索引

javascript - 如何做 javascript/jQuery 文本效果 david desandro

javascript - 如何使用 Jquery 在直接子节点上插入 CSS 代码?

jQuery 文本在 30 秒空闲后消失

javascript - 使用 JavaScript/jQuery 和 AJAX 模拟超时触发器

jquery - Bootstrap DatePicker 使用 onRender 禁用许多日期

javascript - jquery-tmpl 中缺少第一个 html 标签

javascript - Jquery 模板,转义 "${...}"

jquery - 如何调用 jquery-templates 中嵌套的 {{tmpl}} 标签中的函数?

jQuery jquery-tmpl 路线图