I am trying to learn jquery and have a question -
The div element on the page looks like -
one
one.one
one.one.one
Both the below selectors are giving me the same result -
$('div p').css({'background-color' : 'blue'});
$('div>p').css({'background-color' : 'blue'});
Shouldn't the second selector just return only the first Answer What's happening in your code is since the closing So, it's being read as: one one.one one.one.one That's why they all became blue. Demo: http://jsfiddle.net/wP7uD/ Open your browser's dev tools and inspect the DOM, you'll see that there are 5 Moral of this: You cannot have W3C spec for tag of the
$('div p')
selects all tags that are descendants of a
$('div>p')
only selects tags that are direct children of a
(actually 5, since the last 2 closing tags are being "mis-read") tags that are all siblings.
$('div>p')
matched them all, since they are all direct children of the tags.
tags as children of
tags.
tags: http://www.w3.org/TR/html-markup/p.html
No comments:
Post a Comment