亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jquery遍歷時的函數傳參問題

jquery遍歷時的函數傳參問題

拉莫斯之舞 2019-02-05 17:15:53
jquery中遍歷時如何將當前數組的索引值傳入要綁定的click函數中,想用立即執行函數進行傳參,但是寫了一下會報錯,求大神們給個辦法$(".swiper-index .swiper-index-item").each(function(i){       // 如何將i傳入click函數中?    $(this).on("click",function(){        index = i + 1;        $swiper.css("left",-imgWidth*index);    }); }) 
查看完整描述

2 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

其實用不著這么寫,用jq就沒必要按原生的思路寫了。


$(".swiper-index .swiper-index-item").on('click',function(){

    var c_index = $(this).index();

    index = c_index  + 1;

    $swiper.css("left",-imgWidth*index); 

})


查看完整回答
反對 回復 2019-03-01
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

$(".swiper-index .swiper-index-item").each(function(i){   

    (function(i){ 

        $(this).on("click",function(){

             index = i + 1;

             $swiper.css("left",-imgWidth*index);

        }); 

    })(i) 

}) 

或者


$(".swiper-index .swiper-index-item").each(function(i){   

    // 如何將i傳入click函數中?

    $(this).on("click",function(){

        let index = i + 1;

        $swiper.css("left",-imgWidth*index);

    }); 

}) 


查看完整回答
反對 回復 2019-03-01
  • 2 回答
  • 0 關注
  • 490 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號