each(callback)
而且,在每次執(zhí)行函數(shù)時(shí),都會(huì)給函數(shù)傳遞一個(gè)表示作為執(zhí)行環(huán)境的元素在匹配的元素集合中所處位置的數(shù)字值作為參數(shù)(從零開始的整形)。
返回 'false' 將停止循環(huán) (就像在普通的循環(huán)中使用 'break')。返回 'true' 跳至下一個(gè)循環(huán)(就像在普通的循環(huán)中使用'continue')。Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index).
Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).jQuery
callback (Function) : 對(duì)于每個(gè)匹配的元素所要執(zhí)行的函數(shù)
迭代兩個(gè)圖像,并設(shè)置它們的 src 屬性。注意:此處 this 指代的是 DOM 對(duì)象而非 jQuery 對(duì)象。
HTML 代碼:
jQuery 代碼:
結(jié)果:
如果你想得到 jQuery對(duì)象,可以使用 $(this) 函數(shù)。
jQuery 代碼:
你可以使用 'return' 來提前跳出 each() 循環(huán)。
HTML 代碼:
<button>Change colors</button><span></span> <div></div> <div></div><div></div> <div></div><div id="stop">Stop here</div> <div></div><div></div><div></div>
jQuery 代碼:
$("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } });});--------------------------------------------------------------------------------------------------------------------------------Number
計(jì)算文檔中所有圖片數(shù)量
HTML 代碼:
jQuery 代碼:
結(jié)果:
Number
計(jì)算文檔中所有圖片數(shù)量
HTML 代碼:
jQuery 代碼:
結(jié)果:
取得所有匹配的 DOM 元素集合。
如果你想要直接操作 DOM 對(duì)象而不是 jQuery 對(duì)象,這個(gè)函數(shù)非常有用。
Array<Element>
選擇文檔中所有圖像作為元素?cái)?shù)組,并用數(shù)組內(nèi)建的 reverse 方法將數(shù)組反向。
HTML 代碼:
jQuery 代碼:
結(jié)果:
Element
index (Number) :取得第 index 個(gè)位置上的元素
HTML 代碼:
jQuery 代碼:
結(jié)果:
Number
subject (Element) : 要搜索的對(duì)象
返回ID值為foobar的元素的索引值值。
HTML 代碼:
jQuery 代碼:
結(jié)果:
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注