Using jQuery 1.4.2 (IE8 in compatibility mode)
Given the following structure:
<div id='something'>something</div>
<div id='parental'>
<div><p>hi there</p> goats</div>
<p>hello again</p>
</div>
<div> end of the line</div>
and this code:
var Fred= $('#parental');
$('div').css({color: 'blue'});
Fred.children('div').css({color: 'red'});
Fred.children('div').children('p').css({color:'green',border:'solid red 2px'});
Fred.children('div p').css({color: 'orange'});
Fred.children('div>p').css({border:'solid #FFFF00 2px'});
- "something" and " end of the line" are blue as I would expect
- "goats" is red as I would expect.
- "hi there" is green with red border (I expected it to be orange/yellow border)
- "hello again" is orange with yellow border (not what I expected).
Why do the Fred.children('div p')
selector and the Fred.children('div').children('p')
and Fred.children('div>p')
not select the same thing?
See it in action here: http://jsfiddle.net/bxAzN/