码迷,mamicode.com
首页 > Web开发 > 详细

jQuery: .each(function)

时间:2016-05-13 11:24:28      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

Iterate over a jQuery object, executing a function for each matched element.

.each(function)

function

type: Function(Integer index, Element element)

a function to execute for each matched element

 

When called it iterates over the DOM elements that are part of the jQuery object.Each time the callback runs, it is passed the current loop iteration,begginning from 0.

the callback is fired in the context of the current DOM element, so the keyword this refers to the element.this == element

example:

<ul>

  <li>foo</li>

  <li>bar</li>

</ul>

$(‘li‘).each(function(index){

console.log(index + ": " + $(this).text());

})

输出:

0: foo

1: bar

you can stop the loop from within the callback function by returning false.

$( "div" ).each(function( index, element ) {
  // element == this
  $( element ).css( "backgroundColor", "yellow" );
  if ( $( this ).is( "#stop" ) ) {
    $( "span" ).text( "Stopped at div index #" + index );
    return false;
  }
});

 

jQuery: .each(function)

标签:

原文地址:http://www.cnblogs.com/carolina/p/5486110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!