繁体   English   中英

underscore.js在html中不能与<和>一起使用

[英]underscore.js not working with < and > in html

我正在使用underscore.js,我在<%= name %>中遇到<和>问题。 他们会自动被&lt; 和&gt; 这导致我的代码不起作用。

我的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("&lt;","<");
source = source.replace("&gt;",">");

有没有其他方法可以解决我的问题?

传统上,人们使用带有非JavaScript“type”属性的<script>标签来嵌入模板。

<script id=underscore type=text/underscore>
  <%= name %>
</script>

浏览器将完全忽略关闭</script>标记之前的所有内容。 您仍然可以将其作为<script>.innerHTML获取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM