Nodes को Filter करना
In this page:
$(selector).first();
$(selector).last();
$(selector).eq(index);
$(selector).filter(selector);
$(selector).not(selector);
filter()
.filter() किसी मौजूदा jQuery selection को सिर्फ उन elements तक narrow करता है जो आपके दिए गए selector या test function को satisfy करते हैं। find() के उलट, यह कभी descendants को नहीं देखता -- यह सिर्फ current selection में पहले से मौजूद elements को रखता या हटाता है।
उदाहरण: filter()
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<li>One</li><li class="done">Two</li><li class="done">Three</li>
<script>
console.log($("li").filter(".done").length);
</script>
</body>
</html>
not()
not() current selection से किसी selector से match करने वाले elements हटाता है, effectively filter() का उल्टा। यह तब उपयोगी है जब यह describe करना आसान हो कि किन elements को exclude करना है बजाय किन्हें रखना है।
उदाहरण: not()
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<li>One</li><li class="done">Two</li>
<script>
console.log($("li").not(".done").length);
</script>
</body>
</html>
eq()
eq() current selection के अंदर एक specific zero-based index पर element select करता है, किसी array में index करने जैसा। एक negative index pass करना शुरुआत के बजाय selection के अंत से count करता है।
उदाहरण: eq()
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<li>One</li><li>Two</li><li>Three</li>
<script>
console.log($("li").eq(1).text());
</script>
</body>
</html>
first() और last()
first() और last() दो सबसे common filtering tasks -- सिर्फ पहला या आख़िरी matched element लेना -- को eq(0) या एक length calculation की ज़रूरत के बिना जल्दी लिखने लायक बनाते हैं। दोनों equivalent eq() call के shorthand हैं।
उदाहरण: first() and last()
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<li>One</li><li>Two</li><li>Three</li>
<script>
console.log($("li").first().text(), $("li").last().text());
</script>
</body>
</html>
Filtering Practice
Filtering आपको एक बड़ी selection में से सिर्फ उन elements के साथ काम करने में मदद करता है जिनकी असल में आपको ज़रूरत है, जैसे सभी list items को सिर्फ completed marked वालों तक narrow करना। यह बाद के code को एक पहले से precise set पर act करके simpler रखता है।
उदाहरण: Filtering Practice
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<li class="done">Task 1</li><li>Task 2</li>
<script>
console.log($("li").filter(".done").length + " completed");
</script>
</body>
</html>
.filter("li")को.find("li")के साथ confuse करना;.filter()current set को narrow करता है जबकि.find()descendants के लिए उसके अंदर search करता है।- यह भूल जाना कि
.eq()zero-based है, इसलिए$("li").eq(1)दूसरा item select करता है, पहला नहीं। .not(".done")इस्तेमाल करना और उम्मीद करना कि यह page बदल देगा, जबकि यह सिर्फ एक छोटी jQuery selection return करता है और DOM से कोई elements नहीं हटाता।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: