我正在使用 underscore.js 将 JSON 数据http://pastebin.com/WrUfT1Z4呈现到模板中。它在 Firefox、Chrome、Safari、IE9 + 10 中运行良好,但在 ie8 中抛出错误。
“SCRIPT5007:无法获取未定义或空引用函数代码 (2),第 6 行字符 1 的属性‘slug’”
在 IE8 调试器中,突出显示以下内容:
_.each(things,function(thing,key,list){
__p+='\n\t\t\t \n\t\t\t\t\t\t<div class="accordion-heading">\n\t\t\t\t\t\t\t<a class="no-ajaxy accordion-toggle ic-minus block collapsed" data-toggle="collapse" href="#things-'+
((__t=( thing.slug ))==null?'':__t)+
'">\n\t\t\t\t\t\t\t\t'+
((__t=( thing.title ))==null?'':__t)+
'\n\t\t\t\t\t\t\t</a>\n\t\t\t\t\t\t</div> <!-- header -->\n\t\t\t \n\t\t\t\t\t\t<div id="things-'+
((__t=( thing.slug ))==null?'':__t)+
'" class="accordion-body collapse">\n\t\t\t\t\t\t\t<div class="accordion-inner">\n\t\t\t\t\t\t\t\t';
有谁知道可能导致此错误的原因以及是否可以采取任何措施来解决它?这里有一个类似的问题,但我不确定答案如何适用于我的代码(下面的原始代码)。IE7 和 IE8 中的 JavaScript 错误:SCRIPT5007:无法获取属性“newQuestion”的值:对象为空或未定义
谢谢!
这是导致错误的原始下划线模板:
<script type="text/x-underscore" 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>
$(document).ready(function(){
var template = $("#furniture-template").html();
$furnitureList = _.template(template, {things: things});
});