开发者

Can jQuery perform a compound select against the top level only? (a.k.a. "How to avoid chaining children")

开发者 https://www.devze.com 2022-12-29 22:01 出处:网络
Basically, is there a way to write a.childre开发者_开发问答n(\'.outer\').children(\'.inner\') without the intermediate selector?I can\'t write

Basically, is there a way to write

a.childre开发者_开发问答n('.outer').children('.inner')

without the intermediate selector? I can't write

$('.outer > .inner', a)

because I don't want to do full-depth search against a — I know that the .outer elements are immediate children of a.

It's partly a matter of "elegance", but partly because I'm trying to avoid "throwaway" element sets. Yes, jQuery may in effect do the same thing, but it has a better chance of optimizing (at least in theory), when it knows the full query's intent.


You can do this to start with the immediate children, not doing a full-depth search on .outer to start:

$('> .outer > .inner', a)

Or, slightly different, this:

a.find('> .outer > .inner')

You can see a demo of both working here.


The ">" selector is what you need. It will select direct child element, not descendants of all levels: http://api.jquery.com/child-selector/

0

精彩评论

暂无评论...
验证码 换一张
取 消