2013-10-03 40 views
1

我发现这个片段给了我一个对象的最大高度,它在除ie8以外的所有内容中都很好用。有什么我可以改变,这将使其正常工作?这个JS为什么不在ie8中工作?

function thisHeight(){ 
     return $(this).height(); 
    } 

    $("#new-deals ul.centerSpace").height(function() { 
     var dealNameHeight = Math.max.apply(Math, $(this).find(".deal-name").map(thisHeight));  
     $(".deal-name").css({height: dealNameHeight}); 
    }); 
+1

什么不行?你有任何错误? – ComFreek

回答

0

IE8不支持map();作为回退,你可以使用下划线,它提供了一个实现map()它的浏览器不支持它,并允许你传递一个上下文。

这让我介绍第二个问题:您可能遇到必须通过thisHeight传递的上下文问题。

var arr = _.map($(this).find(".deal-name"), function(el) { 
    return thisHeight.call(el); //note that el and this are not identical 
}, this); 
Math.max.apply(Math, arr); //if you use underscore 

如果上下文不超过thisHeight过去了,this很可能指的是全局对象,这是window