ตัวกรอง jQuery () วิธีการ

❮ วิธีการข้ามผ่าน jQuery

ตัวอย่าง

ส่งคืนองค์ประกอบ <p> ทั้งหมดที่มีชื่อคลาส "intro":

$("p").filter(".intro")

ความหมายและการใช้งาน

filter() วิธีการส่งกลับองค์ประกอบที่ตรงกับเกณฑ์บางอย่าง

วิธีนี้ช่วยให้คุณระบุเกณฑ์ได้ องค์ประกอบที่ไม่ตรงกับเกณฑ์จะถูกลบออกจากการเลือก และองค์ประกอบที่ตรงกันจะถูกส่งกลับ

วิธีนี้มักใช้เพื่อจำกัดการค้นหาองค์ประกอบในกลุ่มขององค์ประกอบที่เลือก

เคล็ดลับ:วิธีการ filter() นั้นตรงกันข้ามกับวิธี not ()


ไวยากรณ์

$(selector).filter(criteria,function(index))

Parameter Description
criteria Optional. Specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements.

Tip: To specify multiple criteria, use comma.
function(index Optional. Specifies a function to run for each element in the set. If it returns true, the element is kept. Otherwise, the element is removed.
  • index - The index position of the element in the set
Note: this is the current DOM element.

ลองด้วยตัวคุณเอง - ตัวอย่าง


ใช้ตัวเลือก :even ร่วมกับ filter() เพื่อส่งคืนองค์ประกอบ <p> ทั้งหมดที่เท่ากัน


วิธีส่งคืนองค์ประกอบ <p> ทั้งหมดที่มีคลาส "intro" และ id "outro"


วิธีคืนค่าองค์ประกอบ <p> ทั้งหมดที่มีคลาส "intro" ภายในองค์ประกอบ <div> ด้วยวัตถุ jQuery


วิธีคืนค่า <p> องค์ประกอบที่มี id "intro" ด้วยองค์ประกอบ DOM


วิธีใช้ฟังก์ชันเพื่อเลือกองค์ประกอบ <p> ทั้งหมดที่มีองค์ประกอบ <span> สององค์ประกอบอยู่ภายใน


❮ วิธีการข้ามผ่าน jQuery