each(callback)
而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整形)。
返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'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) : 对于每个匹配的元素所要执行的函数
迭代两个图像,并设置它们的 src 属性。注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。
HTML 代码:
jQuery 代码:
结果:
如果你想得到 jQuery对象,可以使用 $(this) 函数。
jQuery 代码:
你可以使用 'return' 来提前跳出 each() 循环。
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
计算文档中所有图片数量
HTML 代码:
jQuery 代码:
结果:
Number
计算文档中所有图片数量
HTML 代码:
jQuery 代码:
结果:
取得所有匹配的 DOM 元素集合。
如果你想要直接操作 DOM 对象而不是 jQuery 对象,这个函数非常有用。
Array<Element>
选择文档中所有图像作为元素数组,并用数组内建的 reverse 方法将数组反向。
HTML 代码:
jQuery 代码:
结果:
Element
index (Number) :取得第 index 个位置上的元素
HTML 代码:
jQuery 代码:
结果:
Number
subject (Element) : 要搜索的对象
返回ID值为foobar的元素的索引值值。
HTML 代码:
jQuery 代码:
结果:
新闻热点
疑难解答