jQuery判斷選中的第幾個元素:普通函數使用 $('元素對象').index(this) ,箭頭函數使用 $(e.target).index();

當調用函數爲普通函數function(){}時:
 
 $('ul li').mouseover(function () {
                var index = $('ul li').index(this);//獲取當前對象的索引
                console.log('我是第幾' + index + '個');
    })
 
 
 
 
當調用函數爲箭頭函數 ()=>{}時,因爲箭頭函數沒有this,此時需要傳入參數e
 
$('ul li').mouseover((e) => {
                var index = $(e.target).index(); //獲取當前對象的索引
                console.log('我是第幾' + index+ '個');
     })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章