Pergunta

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'?

Foi útil?

Solução

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

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

Outras dicas

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');

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top