.each( function(index, Element) )
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> div { color:red; text-align:center; cursor:pointer; font-weight:bolder; width:300px; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(document.body).click(function () { $("div").each(function (i) { if (this.style.color != "blue") { this.style.color = "blue"; } else { this.style.color = ""; } }); }); }); </script> </head> <body> <div>Click here</div> <div>to iterate through</div> <div>these divs.</div> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> ul { font-size:18px; margin:0; } span { color:blue; text-decoration:underline; cursor:pointer; } .example { font-style:italic; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("span").click(function () { $("li").each(function(){ $(this).toggleClass("example"); }); }); }); </script> </head> <body> To do list: <span>(click here to change)</span> <ul> <li>Eat</li> <li>Sleep</li> <li>Be merry</li> </ul> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> div { width:40px; height:40px; margin:5px; float:left; border:2px blue solid; text-align:center; } span { color:red; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); }); }); </script> </head> <body> <button>Change colors</button> <span></span> <div></div> <div></div> <div></div> <div></div> <div id="stop">Stop here</div> <div></div> <div></div> <div></div> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท