jQuery(each)函数

    科技2025-08-26  18

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

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

    参数callbackFunctionV1.0

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

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

    HTML 代码:        

    jQuery 代码:          $(“img”).each(function(i){                              this.src = “test” + i + “.jpg”;                              });结果:                       [ , ]描述:                       如果你想得到 jQuery对象,可以使用 $(this) 函数。

    HTML 代码:         Change colors

                                  

                                                                                                                                Stop here                                                                                              jQuery 代码:          $(“img”).each(function(){                             $(this).toggleClass(“example”);                               });你可以使用 ‘return’ 来提前跳出 each() 循环。

    HTML 代码:         Change colors

                                  

                                                                                                                                Stop here                                                                                              jQuery 代码:          $(“button”).click(function () {                                $(“div”).each(function (index, domEle) {                              // domEle == this                               ( d o m E l e ) . c s s ( " b a c k g r o u n d C o l o r " , " y e l l o w " ) ;                                                           i f ( (domEle).css("backgroundColor", "yellow");                               if ( (domEle).css("backgroundColor","yellow");                             if((this).is("#stop")) {                              $(“span”).text(“Stopped at div index #” + index);                              return false;                              }                                });                               });

    Processed: 0.010, SQL: 8