jQuery 选择器不是我所期望的
使用 jQuery 1.4.2(兼容模式下的 IE8)
给定以下结构:
<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>
和此代码:
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'});
- “某物”和“行尾”是蓝色的,正如我所期望的
- “山羊”是红色的,正如我所期望的。
- “嗨,那里”是绿色的,带有红色边框(我希望它是橙色/黄色边框)
- “你好,再次”是橙色的,带有黄色边框(不是我所期望的)。
为什么使用 Fred.children('div p')
选择器和 Fred.children('div').children('p')
和 Fred.children('div').children('p')
呢? Children('div>p') 没有选择相同的东西?
在这里查看它的实际效果:http://jsfiddle.net/bxAzN/
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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为
.children()
执行以下操作:现在采用
div p
。#parental
的唯一子元素是div
和p
元素。.children('div p')
仅匹配p
元素,因为它有一个div
作为祖先(#parental
本身)。但div
子元素显然与该选择器不匹配。您应该将
children()
视为获取此选择器过滤的所有子项,这与获取与此选择器匹配的所有后代不同。为此,您必须使用find()
。相比之下,
Fred.children('div').children('p')
首先获取Fred
的所有div
子级,然后选择所有div
的p
子元素。Because
.children()
does the following:Now take
div p
. The only children of#parental
are adiv
and ap
element..children('div p')
only matches thep
element as it has adiv
as ancestor (#parental
itself). But thediv
child clearly not matches this selector.You should think about
children()
as get all children filtered by this selector which is different from get all descendants that match this selector. For this you would have to usefind()
.Fred.children('div').children('p')
in contrast first takes alldiv
children ofFred
and then selects all thediv
'sp
children.Children() 相当于执行“>”在选择器中。
jquery网站说...
将不起作用,因为您试图向下查看 DOM 树的两个阶段。使用
children('div>p')
确实有效,因为它实际上是 1 选择器,尽管我意识到为什么这很令人困惑。如果您想让一个函数完成您想要做的事情,您需要查看
find()
。children() is the equvalent of doing ">" in the selector.
jquery website says it...
therefore using
children('div p')
won't work because you're trying to look two stages down the DOM tree. Usingchildren('div>p')
does work because its effectively 1 selector, although I realise why thats confusing.If you want a function to do what you're trying to do you need to look at
find()
.尝试更改子项以找到
其选择相同的“嗨那里”
http://jsfiddle.net/bxAzN/2/
Children只查看节点的直接子节点,而find会遍历节点下面的整个DOM,因此children会更快。使用哪一个取决于您是否只想考虑直接后代节点或 DOM 中低于该节点的所有节点。 -tvanfosson
Children 和 find 不一定会给出相同的结果:find() 将获得任何后代节点,而 Children() 只会获得匹配的直接子节点。
因此,find() 会更慢,因为它必须搜索可能匹配的每个后代节点,而不仅仅是直接子节点。
——约翰·费米内拉
try changing children to find
its selecting same "hi there"
http://jsfiddle.net/bxAzN/2/
Children only looks at the immediate children of the node, while find traverses the entire DOM below the node, so children will be faster. Which to use depends on whether you only want to consider the immediate descendants or all nodes below this one in the DOM. -tvanfosson
Both children and find won't necessarily give the same result: find() will get you any descendant node, whereas children() will only get you immediate children that match.
Thus, find() will be slower since it must search for every descendant node that could be a match, and not just immediate children.
-John Feminella