javascript - How to find element with class on any depth using JQuery? - Stack Overflow

I know that I have somewhere inside div on any depth ( maybe one or two or three, it changes over time

I know that I have somewhere inside div on any depth ( maybe one or two or three, it changes over time ) with class="nested". How to find that element on any depth using JQuery ?

I tried like

var nest=$('#container_div').find('.nested'); but it doesn't work.

I know that I have somewhere inside div on any depth ( maybe one or two or three, it changes over time ) with class="nested". How to find that element on any depth using JQuery ?

I tried like

var nest=$('#container_div').find('.nested'); but it doesn't work.

Share Improve this question edited May 5, 2012 at 15:24 Damir asked May 5, 2012 at 15:12 DamirDamir 56.3k98 gold badges251 silver badges368 bronze badges 2
  • 2 Other than missing an apostrophe your code should work fine. find() will look for the element no matter how deep it is. – kapa Commented May 5, 2012 at 15:14
  • You're missing a quote. This works fine: $('#container_div .nested') (.find() is not needed). – Rob W Commented May 5, 2012 at 15:15
Add a ment  | 

3 Answers 3

Reset to default 4

And another syntax for fun:

var nest = $( '.nested', '#container_div' );

I give jQuery a context of where to look for the .nested class.

var nest=$('#container_div').find('.nested'); Maybe you forgot quote?

Nothing wrong with the other answer but:

var nest=$('#container_div .nested');

is more concise. Separating selectors with a space is the same as find within a selector. If for some reason you wanted to limit to direct children only,

var nest=$('#container_div > .nested');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744382872a4571516.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信