Possible Duplicate:
Is there a CSS parent selector?
I am able to hide a DOM tree which look in this way by using .closest()
.
<div class='parent'>
<!-- some html code -->
<div class='child'>
</div>
<!-- some html code -->
<div>
$('.child').closest('parent').hide();
It will be possible to get the same effect just by using CSS?
If yes, how?
The closest() method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string.
Element. closest(); This function takes a CSS selector string as an argument. it then returns the closest ancestor of the current element (or the element itself) which matches the CSS selector which was passed in the arguments.
HTML DOM Element closest() MethodThe closest() method searches up the DOM tree for elements which matches a specified CSS selector. The closest() method starts at the element itself, then the anchestors (parent, grandparent, ...) until a match is found. The closest() method returns null() if no match is found.
The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent, great-grandparent, and so on. The DOM tree: This method traverse upwards from the current element, all the way up to the document's root element (<html>), to find the first ancestor of DOM elements.
No selector currently exists that can select a previous or parent element.
There is a level 4 selector that is currently being developed.
So in the future, you may be able to do something like this:
!.parent > .child { display: none; }
But until then, you'll have to stick with
$('.child').parent();
in jQuery.
No. See for yourself, no such selector exists in CSS3, and not in CSS2 either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With