文字
分享

返回值:jQueryeach(callback)

概述

以每一个匹配的元素作为上下文来执行一个函数。

意味着,每次执行传递进来的函数时,函数中的this关键字都指向一个不同的DOM元素(每次都是一个不同的匹配元素)。而且,在每次执行函数时,都会给函数传递一个表示作为执行环境的元素在匹配的元素集合中所处位置的数字值作为参数(从零开始的整型)。 返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。

参数

callbackFunctionV1.0

对于每个匹配的元素所要执行的函数

示例

描述:

迭代两个图像,并设置它们的 src 属性。注意:此处 this 指代的是 DOM 对象而非 jQuery 对象。

HTML 代码:

1

<code><img/><img/></code>

jQuery 代码:

1

2

3

<code>$("img").each(function(i){

   this.src = "test" + i + ".jpg";

 });</code>

结果:

1

<code>[ <img src="test0.jpg" />, <img src="test1.jpg" /> ]</code>

描述:

如果你想得到 jQuery对象,可以使用 $(this) 函数。

HTML 代码:

1

2

3

4

5

6

7

8

9

10

11

12

<code><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></code>

jQuery 代码:

1

2

3

<code>$("img").each(function(){

  $(this).toggleClass("example");

});</code>

描述:

你可以使用 'return' 来提前跳出 each() 循环。

HTML 代码:

1

2

3

4

5

6

7

8

9

10

11

12

<code><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></code>

jQuery 代码:

1

2

3

4

5

6

7

8

9

10

<code>$("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;

  }

});

});</code>

上一篇:jQuery(callback)下一篇:size()