开发者

Reuse cached selector inside a child selector

开发者 https://www.devze.com 2022-12-24 01:24 出处:网络
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开发者_如何
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

精彩评论

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