首页 > Web开发 > 详细

jQuery: .each(function)

时间:2016-05-13 11:22:37      阅读:247      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!