【发布时间】:2011-12-06 20:49:38
【问题描述】:
在jQuery documentation for the child selector我看到了这个注释:
注意:
$("> elem", context)选择器将在未来版本中弃用。因此不鼓励使用它来代替使用替代选择器。
我一直使用这种模式,通常是这样的:
$nodes.find('> children[something=morecomplicated] > somethingelse');
但是,我不明白他们所指的“替代选择器”可能是什么。 编写一个遍历上下文节点的直接子节点的选择器的正确方法是什么? 另外,谁能解释一下为什么这是贬值的?每个人都给出的所有选择看起来都非常丑。
以下是一些不起作用的东西:
// does not guarantee that '.child' is an immediate child
$nodes.find('.child > .grandchild');
// this will return empty array in recent jQuery
// and will return full list of children in older jQuery
$nodes.children('.child > .grandchild');
// Anything like this which forces you to split up the selector.
// This is ugly and inconsistent with usual selector ease-of-use,
// and is a non-trivial conversion for long or complex selectors.
$nodes.children('.child').children('.grandchild');
// After all, no one would ever recommend
$nodes.find('.this').children('.that');
// instead of
$nodes.find('.this > .that');
【问题讨论】:
-
啊,即使经过努力搜索,我也没有看到这个问题。不过,没有一个答案真的很令人满意。
-
你肯定提出了一个很好的观点。但是,除非你发现我目前不知道的东西,否则我认为链接子调用和拆分选择器可能是你唯一的选择。
-
这太可怕了。有谁知道贬低使用初始“>”的理由?我可能想与 jQuery 抗争。
-
等等,他们肯定没有反对使用首字母“>”。正如我在回答中所说:他们刚刚不赞成使用逗号后跟上下文选择器。所以这个jsfiddle.net/DwUTw 仍然很好。我想我们都只是有点困惑。
标签: javascript jquery jquery-selectors