jquery中選擇器的遍歷方法

jquery的遍歷方法是.each()方法,能夠支持多樣化的選擇器遍歷場景。jquery

1.索引值遍歷。dom

$('div').each(function (i) {
    // i就是索引值
    console.log(i);
    // this表示獲取遍歷每個dom對象
    console.log(this);
});

若是該遍歷的dom對象須要調用jquery的方法,可使用$(this)將dom對象轉換爲jquery對象。ide

2.索引值+dom對象遍歷。this

$('div').each(function (index, domEle) {
    // index就是索引值
    console.log(index);
    // domEle表示獲取遍歷每個dom對象
    console.log(domEle);
});

若是該遍歷的dom對象須要調用jquery的方法,可使用$(domEle)將dom對象轉換爲jquery對象。對象

3.更實用的遍歷方法。索引

1)先獲取選擇器集合對象,保存到一個變量yanggb裏。it

var yanggb = $('div');

2)遍歷選擇器集合對象的每個元素。io

$.each(yanggb, function (index,domEle) {
    // yanggb是要遍歷的集合
    console.log(yanggb);
    // index就是索引值
    console.log(index);
    // domEle 表示獲取遍歷每個dom對
    console.log(domEle);
});

 

"我說今晚月色真美,風也溫柔。"console

你要去作一個大人,不要回頭,不要難過。
相關文章
相關標籤/搜索