Not sure if I phrase the question correctly, but I'm trying to target a class within another class, but I've declared the parent class as a variable.

<div class="parent">
  <p class="child">hello world</p>
</div>

var a = $('.parent');
var b = $('.parent .child');

Is there a better way/convetion of declaring variable 'b'? Or targeting classes within variable 'a'?

有帮助吗?

解决方案

If you want to target elements inside of a you can use:

var b = a.find('.child');

其他提示

Since 'a' is now a jquery object, you can use the 'children' method to find a matching node:

var b = a.children('p.child'); or var b = a.children('p');

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top