闭包问题!
折腾了近三天,最后还是万能的百知出来的
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++; };