闭包问题!

折腾了近三天,最后还是万能的百知出来的

  

var obj = [
{ "pg":".p1", "ig":1 , "skipp":2},
{ "pg":".p1", "ig":2 , "skipp":3},
{ "pg":".p1", "ig":3 , "skipp":5},
{ "pg":".p1", "ig":4 , "skipp":8},
{ "pg":".p1", "ig":5 , "skipp":11},
{ "pg":".p1", "ig":6 , "skipp":14},
{ "pg":".p1", "ig":8 , "skipp":17},
{ "pg":".p1", "ig":9 , "skipp":20},
{ "pg":".p1", "ig":11 , "skipp":22},
{ "pg":".p1", "ig":12 , "skipp":24},
{ "pg":".p1", "ig":13 , "skipp":26},
{ "pg":".p2", "ig":1 , "skipp":28},
{ "pg":".p2", "ig":3 , "skipp":30},
{ "pg":".p2", "ig":5 , "skipp":35},
];

//下面是正确的,原因就是闭包,
    for(i=0; i <= objclick.length; ){
        var clickname=objclick[i].pg+" img:eq("+ objclick[i].ig+")";
        $(clickname).click((function(i){
                     return function(){mySwiper.swipeTo(objclick[i].skipp,0);}
                                       })(i));
        i++;
        };


//下面是完全错误滴,因为在调用时,已经不在循环内了。所以会出错
    for(i=0; i <= objclick.length; ){
        var clickname=objclick[i].pg+" img:eq("+ objclick[i].ig+")";
       $(clickname).click(function(){
    mySwiper.swipeTo(objclick[i].skipp,0);
    };
        i++;
        };

 

转载于:https://www.cnblogs.com/tongtianta/p/3335047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值