Visibility Filter Selectors
In this page:
$("selector:visible")
$("selector:hidden")
Visible Elements
':visible' सिर्फ उन elements को रखता है जो इस समय layout space लेते हैं -- यह display:none से hidden या render tree से हटाई गई किसी भी चीज़ को exclude करता है। zero width और height वाला एक element, या जिसका कोई ancestor hidden हो, यह visibility check भी fail करता है।
उदाहरण: Visible Elements
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<p>Shown</p><p style="display:none;">Hidden</p>
<script>
console.log($("p:visible").length);
</script>
</body>
</html>
Hidden Elements
':hidden' :visible का उल्टा है -- यह display:none set वाले elements को match करता है, type="hidden" वाले form elements के साथ, लेकिन सिर्फ visibility:hidden या opacity:0 से styled elements को नहीं।
उदाहरण: Hidden Elements
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<p>Shown</p><p style="display:none;">Hidden</p>
<script>
console.log($("p:hidden").length);
</script>
</body>
</html>
Hidden Items दिखाना
':hidden' से select करना और result पर .show() call करना उस content को reveal करने का एक common pattern है जो hidden शुरू हुआ था, जैसे कोई accordion panel या एक collapsed menu। इस तरह visible और hidden states के बीच toggle करना ज़्यादातर simple show/hide UI interactions का आधार है।
उदाहरण: Show Hidden Items
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<p style="display:none;">Panel</p>
<button id="btn">Show</button>
<script>
$("#btn").on("click", function() {
$("p:hidden").show();
});
</script>
</body>
</html>
Visible Items छुपाना
':visible' से select करना और .hide() call करना आपको किसी group में सिर्फ currently-shown elements को छुपाने देता है, पहले से hidden वाले को untouched छोड़ते हुए। यह pairing आपको बिल्कुल उन elements को hide करने का precise control देती है जो user इस समय देख सकता है, और कुछ नहीं।
उदाहरण: Hide Visible Items
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<p>A</p><p style="display:none;">B</p>
<button id="btn">Hide</button>
<script>
$("#btn").on("click", function() {
$("p:visible").hide();
});
</script>
</body>
</html>
Classes के साथ Visibility
Visibility filters class या attribute selectors के साथ combine होते हैं, जैसे '.item:visible', किसी बड़े group को सिर्फ उस subset तक narrow करने के लिए जो इस समय screen पर है। इस तरह filters को chain करना आपको एक broad group को exactly current UI state से relevant elements तक scope करने देता है।
उदाहरण: Visibility with Classes
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<p class="item">Shown</p><p class="item" style="display:none;">Hidden</p>
<script>
console.log($(".item:visible").length);
</script>
</body>
</html>
- यह मान लेना कि
:hiddenसिर्फdisplay:noneelements को match करता है; यहtype="hidden"inputs और बिना layout size वाले elements को भी match करता है। - यह उम्मीद करना कि
:visibility:hiddenयाopacity:0वाले elements के लिए:visibletrue होगा;:visibleयह देखता है कि element जगह लेता है या नहीं, इसलिए वे फिर भी visible count होते हैं। :hiddenitems पर.show()call करना और उम्मीद करना कि यह किसी parent element में भी बैठे hiding rule को override कर देगा, जबकि parent hidden होने पर child hidden ही रहता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: