簡體   English   中英

在jQuery中將子選擇器與上下文節點一起使用的新方法是什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM