开发者

Reuse cached selector inside a child selector

var $container = $('div#myContainer');
var $panels = $('div#myContainer > div');

Is it possible to reuse the selector I've already cached in $container within the next child selec开发者_如何学Pythontor?


You can do:

var $container = $('div#myContainer');
var $panels = $container.children('div');

This selects only the children like you have currently, using it as the context argument actually calls .find() internally, finding all descendants instead of only direct children.


Yes!

var $container = $('div#myContainer');
var $panels = $('div', $container);

This makes use of the additional context argument with the standard jQuery() function. You can read up on it here: http://api.jquery.com/jQuery/#jQuery1

You could also do this.

var $container = $('div#myContainer');
var $panels = $container.find('div');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜