Home > Web Front-end > JS Tutorial > Detailed explanation of each() method in jquery using examples

Detailed explanation of each() method in jquery using examples

伊谢尔伦
Release: 2017-06-16 16:10:12
Original
1313 people have browsed it

The

each() function is a tool function provided by basically all frameworks. Through it, you can traverse the attribute values ​​​​of objects and arrays and process them. Both jQuery and jQuery objects implement this method. For jQuery objects, the each method is simply delegated: the jQuery object is passed as the first parameter to jQuery's each method. In other words: the each method provided by jQuery is All sub-elements in the object provided by parameter 1 are called one by one. The each method provided by the jQuery object calls the sub-elements inside jQuery one by one.

Here are some common uses of each

1. each handles one-dimensional array

 var arr1 = [ "aaa", "bbb", "ccc" ];      
  $.each(arr1, function(i,val){      
      alert(i);   
      alert(val);
  });
Copy after login

alert( i) Will output 0, 1, 2
alert(val) will output aaa, bbb, ccc

2. Each handles two-dimensional array

 var arr2 = [['a', 'aa', 'aaa'], ['b', 'bb', 'bbb'], ['c', 'cc', 'ccc']]      
  $.each(arr, function(i, item){      
      alert(i);   
      alert(item);      
  });
Copy after login

arr2 For a two-dimensional array, item is equivalent to taking each array in this two-dimensional array.
item[0] is relative to taking the first value in each one-dimensional array
alert(i) will output 0, 1, 2, because this two-dimensional array contains 3 array elements
alert(item) will be output as ['a', 'aa', 'aaa'], ['b', 'bb', 'bbb'], ['c', 'cc', 'ccc']

After slightly changing the processing of this two-digit array

var arr = [['a', 'aa', 'aaa'], ['b', 'bb', 'bbb'], ['c', 'cc', 'ccc']]      
   $.each(arr, function(i, item){      
        $.each(item,function(j,val){
            alert(j);
            alert(val);
     }); 
});
Copy after login

alert(j) will output 0, 1, 2, 0, 1, 2, 0, 1 , 2

alert(val) will output as a, aa, aaa, b, bb, bbb, c, cc, ccc

each processes json data, this each has Even more powerful, you can loop through each attribute

var obj = { one:1, two:2, three:3};      
   each(obj, function(key, val) {      
        alert(key);   
        alert(val);      
   });
Copy after login

Here alert(key) will output one two three
alert(val) will output one, 1, two, 2, three, 3
This Why is the key not a number but an attribute? Because the json format is a set of unordered attributes and values. Since it is unordered, where are the numbers?
And this val is equivalent to obj[key]

ecah processes dom elements. Here, an input form element is used as an example.

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




Then you use each as follows

$.each($("input:hidden"), function(i,val){  
     alert(val);
     alert(i);
     alert(val.name);
     alert(val.value);   
 });
Copy after login

Then, alert(val) will output [object HTMLInputElement] because it is a form element.

alert(i) will output 0, 1, 2, 3

alert(val.name); will output aaa, bbb, ccc, ddd, if you use this. name will output the same result
alert(val.value); will output 111,222,333,444, if this.value is used, the same result will be output

each(callback) With each match The element serves as a context to perform a function.
means that every time the function passed in is executed, the this keyword in the function points to a different DOM element (a different matching element each time). Moreover, every time the function is executed, a numeric value representing the position of the element as the execution environment in the set of matching elements is passed to the function as a parameter (an integer starting from zero). Returning 'false' will stop the loop (just like using 'break' in a normal loop). Return 'true' to jump to the next loop (just like using 'continue' in a normal loop).
The following callback is the callback function, which indicates the operation that should be given when traversing the elements. Let’s look at a simple example below:
Iterate over two images and set their src attributes. Note: here this refers to the DOM object rather than the jQuery object.

each() method specifies a function to run for each matching element.
Tip: Returning false can be used to stop the loop early.
Syntax
$(selector).each(function(index,element)) Parameter Description
function(index,element) Required. Specifies the function to run for each matching element.
•index - the index position of the selector
•element - the current element (the "this" selector can also be used)

$("button").click(function(){ 
$("li").each(function(){ 
alert($(this).text()) 
}); 
});
Copy after login

那怎么跳出each呢 
jquery再遍历选定的对象时候用each比较方便。有种应用是找到里面符合条件的对象后,要跳出这个循环。
javascript的跳出循环一般用break. 
同事遇到这个问题,下意识 的用了break,想跳出这个循环。结果报错 
SyntaxError: unlabeled break must be inside loop or switch 
经查,应该用一个 在回调函数里return false即可,大多数jq的方法都是如此的 

The above is the detailed content of Detailed explanation of each() method in jquery using examples. For more information, please follow other related articles on the PHP Chinese website!

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