jquery 學(xué)習(xí)之一 對象訪問
each()
each(callback)
而且,在每次執(zhí)行函數(shù)時,都會給函數(shù)傳遞一個表示作為執(zhí)行環(huán)境的元素在匹配的元素集合中所處位置的數(shù)字值作為參數(shù)(從零開始的整形)。
返回 'false' 將停止循環(huán) (就像在普通的循環(huán)中使用 'break')。返回 'true' 跳至下一個循環(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
參數(shù)
callback (Function) : 對于每個匹配的元素所要執(zhí)行的函數(shù)
示例
迭代兩個圖像,并設(shè)置它們的 src 屬性。注意:此處 this 指代的是 DOM 對象而非 jQuery 對象。
HTML 代碼:
jQuery 代碼:
this.src = "test" + i + ".jpg";
});
結(jié)果:
如果你想得到 jQuery對象,可以使用 $(this) 函數(shù)。
jQuery 代碼:
$(this).toggleClass("example");
});
你可以使用 '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; } });});--------------------------------------------------------------------------------------------------------------------------------size()
返回值
Number
示例
計算文檔中所有圖片數(shù)量
HTML 代碼:
jQuery 代碼:
結(jié)果:
length
返回值
Number
示例
計算文檔中所有圖片數(shù)量
HTML 代碼:
jQuery 代碼:
結(jié)果:
get()
取得所有匹配的 DOM 元素集合。
如果你想要直接操作 DOM 對象而不是 jQuery 對象,這個函數(shù)非常有用。
返回值
Array<Element>
示例
選擇文檔中所有圖像作為元素數(shù)組,并用數(shù)組內(nèi)建的 reverse 方法將數(shù)組反向。
HTML 代碼:
jQuery 代碼:
結(jié)果:
get(index)
返回值
Element
參數(shù)
index (Number) :取得第 index 個位置上的元素
示例
HTML 代碼:
jQuery 代碼:
結(jié)果:
index(subject)
返回值
Number
參數(shù)
subject (Element) : 要搜索的對象
示例
返回ID值為foobar的元素的索引值值。
HTML 代碼:
jQuery 代碼:
結(jié)果:
相關(guān)文章
jQuery學(xué)習(xí)筆記之jQuery+CSS3的瀏覽器兼容性
這篇文章主要介紹了jQuery學(xué)習(xí)筆記之jQuery+CSS3的瀏覽器兼容性的相關(guān)資料,需要的朋友可以參考下2015-01-01
jquery trigger函數(shù)執(zhí)行兩次的解決方法
這篇文章主要介紹了jquery trigger函數(shù)執(zhí)行兩次的解決方法,詳細(xì)分析了trigger函數(shù)執(zhí)行兩次的原因與響應(yīng)的解決技巧,非常具有實用價值,需要的朋友可以參考下2016-02-02
如何使用jquery控制CSS樣式,并且取消Css樣式(如背景色,有實例)
使用jquery控制CSS樣式,并且取消Css樣式的一個簡單小實例,需要的朋友可以參考下2013-07-07
jquery mobile實現(xiàn)可折疊的導(dǎo)航按鈕
這篇文章主要為大家詳細(xì)介紹了jquery mobile實現(xiàn)可折疊的導(dǎo)航按鈕,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03

