【发布时间】:2013-06-14 13:29:00
【问题描述】:
我正在使用 underscore.js 将 JSON 数据 http://pastebin.com/WrUfT1Z4 呈现到模板中。它在 Firefox、Chrome、Safari、IE9 + 10 中运行良好,但在 ie8 中抛出错误。
"SCRIPT5007: 无法获取未定义或 null 的属性 'slug' 参考功能码(2),第6行字符1"
在 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';
有没有人知道是什么导致了这个错误,是否可以采取任何措施来解决它?这里有一个类似的问题,但我不确定答案如何应用于我的代码(下面的原始代码)。 JavaScript error in IE7 & IE8: SCRIPT5007: Unable to get the value of property 'newQuestion': object is null or undefined
谢谢!
这是导致错误的原始下划线模板:
<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});
});
【问题讨论】:
-
那个奇怪的函数是模板的编译形式。你看过
things和thing吗? -
这是 JSON 数组。 pastebin.com/WrUfT1Z4
-
第 177 行有问题,旧版 IE 不喜欢尾随逗号,它会将它们转换为数组中的
null条目。 -
这就是答案。非常感谢,我正在拉头发。
标签: javascript jquery internet-explorer templates underscore.js