Вопрос

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