What's the difference between the space and > selectors? And possibly related, how can I look for something that's the direct child of something else, and not lower down the descendant line?
+14
A:
For:
<ul>
<li>Item 1</li>
<li>Item 2
<ul>
<li>Item 2.1</li>
<li>Item 2.2</li>
</ul>
</li>
<li>Item 3</li>
</ul>
For example
$("ul > li").addClass("blah");
adds class "blah" to 1 2 and 3 whereas:
$("ul li").addClass("blah");
add class "blah" to every list element.
I'm not sure what you're referring to with < and ? operators.
cletus
2009-08-02 03:22:39
Does ul > li set all of them to "blah"? because you have a sub list which also has li children in a ul.
Nick Berardi
2009-08-12 17:04:24
> means immediate children, not grandchildren or more.
rpflo
2009-08-12 17:16:47
ul > li matches the nested lists as well
Ty W
2009-12-09 19:22:42
You should update your example to have the inner `ul` as an `ol` since your current code will not function as you describe (though technically correct).
Joel Potter
2009-12-09 19:28:44
+3
A:
In CSS, > means "direct child of": only nodes that are direct children are selected.
While a space means "any descendant of": direct children and children of those children could be selected.
I would wager jQuery uses the same convention.
Chris Holland
2009-08-12 16:57:34
A:
As already mentioned, a space will select any descendant, whereas >
will select only immediate children. If you want to select only grandchildren or great-grandchildren, then you could use this:
#foo > * > * > .bar
(all elements with class "bar" which are great grandchildren of the element with id "foo")
nickf
2009-11-30 01:49:09
A:
look at this..
$(".testit > a") //match the first <a> tag below
$(".testit a") // matches all <a> tag below
<p class="testit">
<a href="#">All the rules will match this</a>
<span>
<a href="#">second rule can only select this</a>
</span>
</p>
joberror
2010-01-07 00:35:00