15
2009
การใช้งาน Selector Filters แบบ first
สวัสดีครับ วันนี้เรามาทำความรู้จักกับการใช้งาน Selector ของ jQuery กันครับ
jQuery นั้นมีทั้งเครื่องมี Function, Plugin ให้ใช้มากเลยครับ อย่างในบทความนี้ก็เป็นการใช้งาน Selector ที่ jQuery มีมาให้ครับ
Selector คืออะไร ผมจะยกตัวอย่างง่ายๆของการใช้งาน Selector ให้ดูกัน
อย่างเช่น ถ้าเรามี Table อยู่หนึ่ง Table โครงสร้างเป็นดังนี้
1 2 3 4 5 | <table> <tr><td>Row 1</td></tr> <tr><td>Row 2</td></tr> <tr><td>Row 3</td></tr> </table> |
ใน code ด้านบนนี้จะเห็นว่าเรามี TR ทั้งหมด 3 Row ด้วยกัน เราสามารถจะเข้าไปใส่ style sheet ให้กับ TR ได้โดยตรง ถ้าเราไม่ใช้ jQuery Selector จะได้ code แบบนี้นะครับ
1 2 3 | ... <tr style="color:blue; font-weight:bold;"><td>Row 1</td></tr> ... |
ผลที่แสดงออกมาก็คือ ตัวหนังสือที่อยู่ใน Row 1 นั้นจะเป็นตัวหนาและมีสีน้ำเงิน
| Row 1 |
| Row 2 |
| Row 3 |
ในลักษณะเดียวกัน เราสามารถที่จะใช้ selector ของ jQuery นั้นทำการกำหนด style ให้กับ TR ได้โดยที่ไม่ต้องไปเปลี่ยน หรือเพิ่ม code ใน tag TR เลย เราสามารถกำหนดได้ด้วย selector เลย แต่ Table 1 Table นั้นมันก็มีหลาย TR ใน Table เดียวดังนั้น ถ้าเราต้องการจะให้ TR ตัวใหนเปลี่ยนสี หรือกำหนด style แบบเจาะจง เฉพาะ TR นั้น เราจะต้องใช้ filter ช่วยอีกแรงดังนี้ครับ
1 2 3 | $(document).ready(function(){ $("tr:first").css("font-style", "italic"); }); |
จาก code ด้านบนจะเห็นว่ามีการใช้ filters ช่วยในการค้นหา TR ที่มีอยู่ใน Table หลายๆ TR โดยที่จะทำการเลือกเอา TR แรกสุดมาใส่ style ตัวเองเข้าไปผลที่ได้ก็ตามนี้เลยครับ
| Row 1 |
| Row 2 |
| Row 3 |
Final Code :
1 2 3 4 5 6 7 8 9 10 11 | <table border="1" width="30%"> <tr><td>Row 1</td></tr> <tr><td>Row 2</td></tr> <tr><td>Row 3</td></tr> </table> <script> $(document).ready(function(){ $("tr:first").css("font-style", "italic"); }); </script> |
Related Posts
Tags
Facebook Development
Google+ Page
Categories
- HTML5 & CSS3 (8)
- jQuery 1.4 (9)
- jQuery 1.5 (6)
- .data (1)
- jQuery 1.6 (3)
- .data (1)
- jQuery 1.7 (4)
- Events (1)
- jQuery Mobile (3)
- jQuery UI (3)
- Mobile (1)
- News (8)
- Others (28)
- Plugin (15)
- The basics of jQuery (9)
- Tips (13)
- คู่มือการใช้งาน jQuery ฉบับ ภาษา ไทย (30)
- Ajax (4)
- Attributes (1)
- Core (1)
- Deferred (3)
- Effects (7)
- Events (3)
- Manipulation (2)
- Selectors (5)
- Traversing (2)
- Utilities (2)

An article by







