jQuery 选择器不是我所期望的

发布于 2024-09-24 15:57:10 字数 1080 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-10-01 15:57:10

因为 .children() 执行以下操作:

获取匹配元素集中每个元素的子元素,可以选择通过选择器进行过滤。

现在采用div p#parental 的唯一子元素是 divp 元素。 .children('div p') 仅匹配 p 元素,因为它有一个 div 作为祖先(#parental 本身)。但 div 子元素显然与该选择器不匹配。
您应该将 children() 视为获取此选择器过滤的所有子项,这与获取与此选择器匹配的所有后代不同。为此,您必须使用 find()

相比之下,Fred.children('div').children('p') 首先获取 Fred 的所有 div 子级,然后选择所有divp 子元素。

Because .children() does the following:

Get the children of each element in the set of matched elements, optionally filtered by a selector.

Now take div p. The only children of #parental are a div and a p element. .children('div p') only matches the p element as it has a div as ancestor (#parental itself). But the div 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 use find().

Fred.children('div').children('p') in contrast first takes all div children of Fred and then selects all the div's p children.

赏烟花じ飞满天 2024-10-01 15:57:10

Children() 相当于执行“>”在选择器中。

jquery网站说...

仅向下移动一层
DOM树

将不起作用,因为您试图向下查看 DOM 树的两个阶段。使用 children('div>p') 确实有效,因为它实际上是 1 选择器,尽管我意识到为什么这很令人困惑。

如果您想让一个函数完成您想要做的事情,您需要查看 find()

children() is the equvalent of doing ">" in the selector.

jquery website says it...

only travels a single level down the
DOM tree

therefore using children('div p') won't work because you're trying to look two stages down the DOM tree. Using children('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().

情绪操控生活 2024-10-01 15:57:10

尝试更改子项找到

var Fred= $('#parental');
$('div').css({
    color: 'blue'
});
Fred.children('div').css({
    color: 'red'
});
Fred.find('div').find('p').css({
    color: 'green',border:'solid red 2px'
});
Fred.find('div p').css({
    color: 'orange'});

Fred.find('div>p').css({
    border:'solid #FFFF00 2px'
});​

其选择相同的“嗨那里”

http://jsfiddle.net/bxAzN/2/

Children只查看节点的直接子节点,而find会遍历节点下面的整个DOM,因此children会更快。使用哪一个取决于您是否只想考虑直接后代节点或 DOM 中低于该节点的所有节点。 -tvanfosson

Children 和 find 不一定会给出相同的结果:find() 将获得任何后代节点,而 Children() 只会获得匹配的直接子节点。

因此,find() 会更慢,因为它必须搜索可能匹配的每个后代节点,而不仅仅是直接子节点。
——约翰·费米内拉

try changing children to find

var Fred= $('#parental');
$('div').css({
    color: 'blue'
});
Fred.children('div').css({
    color: 'red'
});
Fred.find('div').find('p').css({
    color: 'green',border:'solid red 2px'
});
Fred.find('div p').css({
    color: 'orange'});

Fred.find('div>p').css({
    border:'solid #FFFF00 2px'
});​

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文