Home > Web Front-end > JS Tutorial > jQuery.each() usage sharing_jquery

jQuery.each() usage sharing_jquery

WBOY
Release: 2016-05-16 17:51:22
Original
1101 people have browsed it

Iterates over an array, using both element index and content. (i is the index, n is the content)

Copy code The code is as follows:

$.each ( [0,1,2], function(i, n){
alert( "Item #" i ": " n );
});

Example object , using both member names and variable contents. (i is the member name, n is the variable content)
Copy code The code is as follows:

$ .each( { name: "John", lang: "JS" }, function(i, n){
alert( "Name: " i ", Value: " n );
});

Example the DOM element. Here we take an input form element as an example.

If you have a piece of code like this in your dom




Then you use each as follows
Copy the codeThe code is as follows:

$.each($("input:hidden"), function(i,val){
alert(val); //Output [object HTMLInputElement] because it is a form element.
alert(i); //The output index is 0, 1, 2, 3
alert(val.name); //Output the value of name
alert(val.value); //Output value The value of Iteration method can be used to iterate objects and arrays.

Unlike the $().each() method that iterates over jQuery objects, this method can be used to iterate over any object. The callback function has two parameters: the first is the member of the object or the index of the array, and the second is the corresponding variable or content. If you need to exit the each loop, you can make the callback function return false, and other return values ​​will be ignored.

Parameters
objectObject
The object or array to be traversed.

callback (optional)Function
Callback function executed by each member/element.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template