jQuery eq()เมธอด
ตัวอย่าง
เลือก <p> องค์ประกอบที่สอง (ดัชนีหมายเลข 1):
$("p").eq(1).css("background-color", "yellow");
ความหมายและการใช้งาน
eq() วิธีการส่งกลับองค์ประกอบที่มีหมายเลขดัชนีเฉพาะขององค์ประกอบที่เลือก
หมายเลขดัชนีเริ่มต้นที่ 0 ดังนั้นองค์ประกอบแรกจะมีหมายเลขดัชนี 0 (ไม่ใช่ 1)
ไวยากรณ์
$(selector).eq(index)
Parameter | Description |
---|---|
index | Required. Specifies the index of the element. Can either be a positive or negative number. Note: Using a negative number will start the index count from the end of the selected elements, instead of the beginning. |
ลองด้วยตัวคุณเอง - ตัวอย่าง
การใช้จำนวนลบเพื่อส่งกลับองค์ประกอบ <p> ที่สองจากจุดสิ้นสุดขององค์ประกอบที่เลือก