Underscore.js模板在_.each()函数中的ie8中抛出错误

时间:2013-06-14 13:29:00

标签: javascript jquery internet-explorer templates underscore.js

我正在使用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});
});

1 个答案:

答案 0 :(得分:1)

mu太短的问题指出,我的JSON数组末尾有一个尾随逗号导致错误。删除逗号解决了问题。