gpt4 book ai didi

Jquery tmpl 从函数返回 HTML

转载 作者:行者123 更新时间:2023-12-01 04:57:47 24 4
gpt4 key购买 nike

我有以下模板

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com