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

Detailed explanation of each() method in jquery using examples

伊谢尔伦
伊谢尔伦Original
2017-06-16 16:10:121300browse

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);
  });

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);      
  });

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);
     }); 
});

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);      
   });

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
faccfcae027ff734f73dd06266eca48e
cc896bc9d5d56de012ad70ceb9db5a87
a64ed2e9863cf3d87033948a4a36efde
4fffe49a5b668cdd8d1f20439e1143d9
Then you use each as follows

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

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()) 
}); 
});

那怎么跳出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!

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