[英]What is the new proper way to use a child selector with a context node in jQuery?
In the jQuery documentation for the child selector I saw this note: 在子选择器的jQuery文档中,我看到了这个注释:
Note: The
$("> elem", context)
selector will be deprecated in a future release. 注意:$("> elem", context)
选择器将在以后的版本中弃用。 Its usage is thus discouraged in lieu of using alternative selectors. 因此不鼓励使用其替代选择器。
I use this pattern all the time, usually like this: 我一直使用这种模式,通常是这样的:
$nodes.find('> children[something=morecomplicated] > somethingelse');
However, I don't understand what the "alternative selectors" they refer to could be. 但是,我不明白他们所指的“替代选择者”是什么。 What is the right way to write a selector which traverses the immediate children of a context node? 编写遍历上下文节点的直接子节点的选择器的正确方法是什么? As a bonus, can anyone explain why this is depreciated? 作为奖励,任何人都可以解释为什么这是折旧的? All the alternatives everyone is giving seem amazingly ugly . 每个人都给予的所有替代品看起来都非常难看 。
Here are some things that don't work: 这里有一些事情不工作:
// 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');
The reason they are saying: 他们说的原因是:
Note: The
$("> elem", context)
selector will be deprecated in a future release. 注意:$("> elem", context)
选择器将在以后的版本中弃用。 Its usage is thus discouraged in lieu of using alternative selectors. 因此不鼓励使用其替代选择器。
Is due to the comma followed by the context in the selector. 是由于逗号后跟选择器中的上下文。 Eg $("> elem")
is fine however, $("> elem", context)
will be deprecated. 例如, $("> elem")
很好,但是, $("> elem", context)
将被弃用。
$("> elem", context)
is the same as $(context + "> elem")
. $("> elem", context)
与$(context + "> elem")
。
A correct way of obtaining children and grandchildren is 获得子孙的正确方法是
$("elem").children('.child').children('.grandchild');
or 要么
context.children('.child').children('.grandchild');
or 要么
context.find('> .child > .grandchild');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.