.each() | Hàm JQuery | Tham Khảo JQuery | Học Web Chuẩn
Có thể bạn quan tâm
- Trang chủ
- Tham khảo
- jQuery
- jQuery - function
- .each()
Định nghĩa và sử dụng
- .each(): Thực hiện một hành động cho mỗi phần tử, để làm được điều này ta cần sử dụng phương thức $(this).
Cấu trúc
- Đã được thêm vào từ phiên bản 1.0
.each(function(){}
$('li').each(function(index){ $(this).click(function(){ $(this).addClass("test"+index); }); });.each(function(){}
Html viết:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <style> .list0 { color: red; } .list1 { color: blue; } .list2 { color: yellow; } .list3 { color: green; } .list4 { color: gray; } .list5 { color: pink; } </style> <script> $(function(){ $('li').each(function(index){ $(this).click(function(){ $(this).addClass("list"+index); }); }); }); </script> </head> <body> <ul> <li>list 01</li> <li>list 02</li> <li>list 03</li> <li>list 04</li> <li>list 05</li> <li>list 06</li> </ul> </body> </html>Hiển thị trình duyệt:
Khi sử dụng .each(), phương thức click sẽ chỉ tác động lên từng phần tử riêng lẻ (click lên từng phần tử li để thấy kết quả).
So sánh code HTML trước và sau khi có jQuery:
| Trước khi có jQuery | Sau khi có jQuery - hover lên từng li |
|---|---|
<ul> <li>list 01</li> <li>list 02</li> <li>list 03</li> <li>list 04</li> <li>list 05</li> <li>list 06</li> </ul> | <ul> <li class="list0">list 01</li> <li class="list1">list 02</li> <li class="list2">list 03</li> <li class="list3">list 04</li> <li class="list4">list 05</li> <li class="list5">list 06</li> </ul> |
Ví dụ thêm
Html viết:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <style> .over { color: red; } </style> <script> $(function(){ $('li').each(function(){ $(this).hover(function(){ $(this).addClass("over"); },function(){ $(this).removeClass("over"); }); }); }); </script> </head> <body> <ul> <li>list 01</li> <li>list 02</li> <li>list 03</li> <li>list 04</li> <li>list 05</li> <li>list 06</li> </ul> </body> </html>Hiển thị trình duyệt:
Khi sử dụng .each(), phương thức hover sẽ chỉ tác động lên từng phần tử riêng lẻ (hover lên từng phần tử li để thấy kết quả).
So sánh code HTML trước và sau khi có jQuery:
| Trước khi có jQuery | Sau khi có jQuery - hover lên từng li |
|---|---|
<ul> <li>list 01</li> <li>list 02</li> <li>list 03</li> <li>list 04</li> <li>list 05</li> <li>list 06</li> </ul> | <ul> <li>list 01</li> <li class="over">list 02</li> <li>list 03</li> <li>list 04</li> <li>list 05</li> <li>list 06</li> </ul> |
Ví dụ thêm
Html viết:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Tiêu đề</title> <script src="https://code.jquery.com/jquery-latest.js"></script> <style> .over { color: red; } </style> <script> $(function(){ $('li').each(function(){ $(this).hover(function(){ $(this).addClass("over"); },function(){ $(this).removeClass("over"); }); if ($(this).is("#stop")){ return false; } }); }); </script> </head> <body> <ul> <li>list 01</li> <li>list 02</li> <li>list 03</li> <li id="stop">list 04</li> <li>list 05</li> <li>list 06</li> </ul> </body> </html>Hiển thị trình duyệt:
Sử dụng return false; tại vị trí <li id="stop"> sẽ làm dừng không xử lý các hành động từ thành phần #stop trở đi.
.die()
.empty()
HTML & XHTML
HƯỚNG DẪN HỌC
- Hướng dẫn học
- Hướng dẫn học XHTML & HTML5
- Hướng dẫn học CSS
- Hướng dẫn học CSS3
- Hướng dẫn học Responsive
- Hướng dẫn học ES6
- Hướng dẫn học React.js
- Hướng dẫn học jQuery
- Hướng dẫn học PHP
- Hướng dẫn học Laravel
- Hướng dẫn học Wordpress
- Hướng dẫn học Webpack
- Hướng dẫn học SCSS
THAM KHẢO
Hàm jQuery (Selectors)
- .add()
- .addBack()
- .addClass()
- .after()
- .andSelf()
- .animate()
- .append()
- .appendTo()
- .attr()
- .before()
- .bind()
- .blur()
- .change()
- .children()
- .clearQueue()
- .click()
- .clone()
- .closest()
- .contents()
- .context
- .css()
- .dblclick()
- .delay()
- .delegate()
- .dequeue()
- .detach()
- .die()
- .each()
- .empty()
- .end()
- .eq()
- .error()
- .fadeIn()
- .fadeOut()
- .fadeTo()
- .fadeToggle()
- .filter()
- .find()
- .finish()
- .first()
- .focus()
- .focusin()
- .focusout()
- .get()
- .has()
- .hasClass()
- .height()
- .hide()
- .hover()
- .html()
- .index()
- .innerHeight()
- .innerWidth()
- .insertAfter()
- .insertBefore()
- .is()
- .jquery
- .keydown()
- .keypress()
- .keyup()
- .last()
- .length
- .live()
- .load()
- .map()
- .mousedown()
- .mouseenter()
- .mouseleave()
- .mousemove()
- .mouseout()
- .mouseover()
- .mouseup()
- .next()
- .nextAll()
- .nextUntil()
- .not()
- .off()
- .offset()
- .offsetParent()
- .on()
- .one()
- .outerHeight()
- .outerWidth()
- .parent()
- .parents()
- .parentsUntil()
- .position()
- .prepend()
- .prependTo()
- .prev()
- .prevAll()
- .prevUntil()
- .remove()
- .removeAttr()
- .removeClass()
- .removeData()
- .removeProp()
- .replaceAll()
- .replaceWith()
- .resize()
- .scroll()
- .scrollLeft()
- .scrollTop()
- .select()
- .selector()
- .serialize()
- .serializeArray()
- .show()
- .siblings()
- .size()
- .slice()
- .slideDown()
- .slideToggle()
- .slideUp()
- .stop()
- .submit()
- .text()
- .toArray()
- .toggle()
- .toggleClass()
- .trigger()
- .triggerHandler()
- .unbind()
- .undelegate()
- .unload()
- .unwrap()
- .val()
- .width()
- .wrap()
- .wrapAll()
- .wrapInner()
Tham khảo CSS
- CSS - Tham khảo
- CSS - Bộ chọn (selectors)
- CSS - Thuộc tính
- CSS3 - Tham khảo
- CSS3 - Bộ chọn (selectors)
- CSS3 - Thuộc tính
- Xem thêm ví dụ về CSS
Tham khảo HTML/XHTML
- Tham khảo HTML/XHTML
- Tag theo function
- Tag theo giá trị DTD
- Tham khảo HTML4/XHTML
- Tham khảo HTML5
- Xem thêm ví dụ về HTML
Tham khảo JQUERY
- jQuery - Tham khảo
- jQuery - Cài đặt và sử dụng
- jQuery - Bộ chọn (selectors)
- jQuery - Hàm (function)
- jQuery - Ajax
Tham khảo Thêm
- Lang codes
- Font chữ
- Mã ký tự
- MIME
- Đơn vị trong HTML & CSS
- Giá trị màu trong HTML & CSS
- Thuộc tính tổng quát
CHUYÊN ĐỀ
- Chuyên đề
- Chuyên đề HTML/CSS
- Chuyên đề HTML5/CSS3
- Chuyên đề jQuery/JS
- jQuery/JS plugin
GÓP Ý - LIÊN HỆ
- BẠN SẼ TÀI TRỢ?DONATE
- CÔNG CỤ TẠO CSS3CSS3 GENERATOR
- BỘ CÔNG CỤGENERATOR TOOLS
- CÔNG CỤ HỖ TRỢWEB TOOLS
- CÔNG CỤ TẠO RANDOMRANDOM GENERATOR
- CÔNG CỤ KIỂM TRA RESPONSIVE TEST
- CHIA SẺ HAYWEB & TOOLS
Từ khóa » Hàm Each Trong Jquery
-
5 JQuery.each() Function Examples - Viblo
-
Jquery Each Là Gì? Cùng Tìm Hiểu Về Method Each() Trong Jquery
-
.each Trong Jquery - Hoclaptrinh
-
JQuery.each() | JQuery API Documentation
-
.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


