通过ajax加载下划线模板会丢失html,需要转义"<";>"
Loading underscore template via ajax loses html, need to escape "<", ">"
我使用ajax .js (https://github.com/browserstate/ajaxify/blob/master/ajaxify-html5.js)加载页面内容。默认情况下,该脚本从页面异步加载脚本,如下所示:
$scripts.each(function(){
var $script = $(this),
scriptText = $script.text(),
scriptType = $script.attr('type'),
scriptId = $script.attr('id');
scriptNode = document.createElement('script');
if(scriptType) {
$(scriptNode).attr('type', scriptType);
}
if(scriptId) {
$(scriptNode).attr('id', scriptId);
}
scriptNode.appendChild(document.createTextNode(scriptText));
contentNode.appendChild(scriptNode);
});
问题是,对于我的下划线模板,使用$script.text()
完全忽略了模板的HTML内容。
$script.html()
转义模板代码中的<
和>
字符(下划线的<%
等…),输出最终看起来像:
<% _.each(things,function(thing,key,list){ %>
我如何得到脚本的内部html/文本完整,使其功能正常与AJAX加载?谢谢!
完整的模板脚本如下:
<script type="text/html" id='furniture-template'>
<div class="accordion collapse">
<div class="accordion-group">
<% _.each(things,function(thing,key,list){ %>
<div class="accordion-heading">
<a class="no-ajaxy accordion-toggle ic-minus block collapsed" data-toggle="collapse" href="#things-<%= thing.slug %>">
<%= thing.title %>
</a>
</div> <!-- header -->
<div id="things-<%= thing.slug %>" class="accordion-body collapse">
<div class="accordion-inner">
<% for(var item in thing.items) { %>
<div class="item">
<% if( thing.items[item].images == true ) { %>
<a class="no-ajaxy" data-target="<%= thing.items[item].slug %>-gal" class="img-link ic-cam fl" title="View an example"></a>
<% } %>
<a
class="item-add ic-plus"
data-title="<%= thing.items[item].title %>"
data-slug="<%= thing.items[item].slug %>"
data-img="<%= thing.items[item].images %>"
data-shorthand="<%= thing.items[item].shorthand %>"
data-price="<%= thing.items[item].price %>"
>
<%= thing.items[item].title %>
</a>
</div>
<% } %>
</div> <!-- inner -->
</div> <!-- accordion-body -->
<% }); %>
</div>
</div>
</script>
奇怪的情况。简单的解决方案是使用.html()
和未转义的转义下划线序列,这可能是你想要的(也就是说,你可能不想对所有内容进行未转义):
$script.html().replace(/<%/gi, '<%').replace(/%>/gi, '%>')
你没试过吗:
contentNode.innerHTML = scriptnode;
相关文章:
- 在JavaScript中输出转义字符
- 转义符不能与innerHTML一起使用
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- Rails 3.2 js.erb文件转义js
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- 转义在jsp's脚本
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- "工具提示"jQuery插件坏了
- "锻造;React中的表达式