Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.closest() in CSS [duplicate]

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?

like image 966
Lorraine Bernard Avatar asked Oct 03 '12 12:10

Lorraine Bernard


People also ask

What is E target closest?

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.

What does the element closest () method take as its argument and what does it return?

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.

How do you use the closest element?

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.

What is the use of closest?

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.


2 Answers

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.

like image 105
Matt Kieran Avatar answered Oct 05 '22 05:10

Matt Kieran


No. See for yourself, no such selector exists in CSS3, and not in CSS2 either.

like image 45
Josh Davenport-Smith Avatar answered Oct 05 '22 07:10

Josh Davenport-Smith