.each() | JQuery API Documentation
Có thể bạn quan tâm
The .each() method is designed to make DOM looping constructs concise and less error-prone. 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, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.
Suppose you have a simple unordered list on the page:
| 1 2 3 4 | <ul> <li>foo</li> <li>bar</li></ul> |
You can select the list items and iterate across them:
| 1 2 3 | $( "li" ).each(function( index ) { console.log( index + ": " + $( this ).text() );}); |
A message is thus logged for each item in the list:
0: foo 1: bar
You can stop the loop from within the callback function by returning false.
Note: most jQuery methods that return a jQuery object also loop through the set of elements in the jQuery collection — a process known as implicit iteration. When this occurs, it is often unnecessary to explicitly iterate with the .each() method:
| 1 2 3 4 5 6 7 | // The .each() method is unnecessary here:$( "li" ).each(function() { $( this ).addClass( "foo" );}); // Instead, you should rely on implicit iteration:$( "li" ).addClass( "bar" ); |
Từ khóa » Hàm Each Trong Jquery
-
5 JQuery.each() Function Examples - Viblo
-
.each() | Hàm JQuery | Tham Khảo JQuery | Học Web Chuẩn
-
Jquery Each Là Gì? Cùng Tìm Hiểu Về Method Each() Trong Jquery
-
.each Trong Jquery - Hoclaptrinh
-
JQuery.each() | JQuery API Documentation
-
Sử Dụng Hàm JQuery .each () (với Các Ví Dụ)
-
Hiểu Nhanh Về .forEach () So Với .each () Của JQuery - HelpEx
-
5 JQuery.each() Function Examples - SitePoint
-
What Is The Use Of .each() Function In JQuery ? - GeeksforGeeks
-
What Is Each() Function In JQuery? How Do You Use It?
-
JQuery Và Hàm Each
-
Vòng Lặp .each() - CodeHub
-
JQuery.each( Array, Callback ) - W3cubDocs
-
Adding Jquery 'each' Function To Js Function - Javascript - Stack Overflow