数组叠加的一个思路

var a =[1,10,50,42,112];

/****************************************/
if(t < a[0]){
    tabMenu.removeClass('active');
    tabMenu.eq(0).addClass('active');
}
if(t < a[0]+a[1]){
    tabMenu.removeClass('active');
    tabMenu.eq(1).addClass('active');
}
if(t < a[0]+a[1]+a[2]){
   tabMenu.removeClass('active');
  tabMenu.eq(2).addClass('active'); } if(t < a[0]+a[1]+a[2]+a[3]){ tabMenu.removeClass('active'); tabMenu.eq(3).addClass('active'); } /****************************************/ /**简化后****/ var count = 0; for(var i=0; i<a.length; i++){ count = count+a[i]; if(t < count){ tabMenu.removeClass('active');   tabMenu.eq(i).addClass('active'); } break; }

注: break 

posted @ 2014-12-12 18:58  vivi.wang  阅读(299)  评论(0编辑  收藏  举报