Home  >  Article  >  Web Front-end  >  Usage examples of each() method in jQuery_jquery

Usage examples of each() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:24:091245browse

The example in this article describes the usage of each() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method can execute a function with each element in the matching element set as the context.

Whenever the function is executed, the execution environment of the function is a different DOM element in the matching element collection, and the index of this element in the collection will be passed to this function, and the index value starts from 0. Returning false can be used to stop the loop early. Returning true will continue to execute the function until every element in the matching element is traversed.
The each() method is different from the jQuery.each() method. The each() method can only traverse JQuery objects, while the jQuery.each() method can traverse any object.
Grammar structure:

Copy code The code is as follows:
$(selector).each(function(index,element))

Parameter list:

参数 描述
function(index,element) 每个匹配元素定义运行的方法。
index - 可选。当前上下文元素在匹配元素集合中的索引。
element -可选。当前的元素(也可使用 "this" 选择器)。

Example code:

Copy code The code is as follows:






each() function-Script Home





  • Backstage area

  • Front desk area

  • Database Area

  • Webmaster Zone





The above code can facilitate each element in the li element collection, and then return the text in the li element.

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn