.filter( selector ) .filter( function(index) ) .filter( element ) .filter( jQuery object )
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> div { width:60px; height:60px; margin:5px; float:left; border:2px white solid;} </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("div").css("background", "#c8ebcc") .filter(".middle") .css("border-color", "red"); }); </script> </head> <body> <div></div> <div class="middle"></div> <div class="middle"></div> <div class="middle"></div> <div class="middle"></div> <div></div> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> div { width:60px; height:60px; margin:5px; float:left; border:3px white solid; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("div").css("background", "#b4b0da") .filter(function (index) { return index == 1 || $(this).attr("id") == "fourth"; }) .css("border", "3px double red"); }); </script> </head> <body> <div id="first"></div> <div id="second"></div> <div id="third"></div> <div id="fourth"></div> <div id="fifth"></div> <div id="sixth"></div> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท