[英]What is the new proper way to use a child selector with a context node in jQuery?
在子選擇器的jQuery文檔中,我看到了這個注釋:
注意:
$("> 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');
他們說的原因是:
注意:
$("> elem", context)
選擇器將在以后的版本中棄用。 因此不鼓勵使用其替代選擇器。
是由於逗號后跟選擇器中的上下文。 例如, $("> elem")
很好,但是, $("> elem", context)
將被棄用。
$("> elem", context)
與$(context + "> elem")
。
獲得子孫的正確方法是
$("elem").children('.child').children('.grandchild');
要么
context.children('.child').children('.grandchild');
要么
context.find('> .child > .grandchild');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.