我正在使用 underscore.js,我在 <%= name %>
中遇到了 < 和 > 的问题。 .它们会自动替换为 < ; >这导致我的代码无法工作。
我的 index.html:
<div id="underscore">
<%= name %>
</div>
我的脚本:
var source = $("#underscore").html();
var compiled = _.template(source);
var html = compiled({name: 'pepe'});
$("#underscore").html(html);
我在我的脚本中添加了这行(在 var 编译之前)解决了这个问题并且工作正常
source = source.replace("<","<");
source = source.replace(">",">");
还有其他方法可以解决我的问题吗?
最佳答案
通常人们使用 <script>
带有非 JavaScript“类型”属性的标签以嵌入模板。
<script id=underscore type=text/underscore>
<%= name %>
</script>
浏览器将完全忽略直到结束 </script>
的所有内容标签。您仍然可以将其作为 .innerHTML
获取的 <script>
.
关于javascript - underscore.js 不能在 html 中使用 < 和 >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19711882/