开发者

Accessing jquery objects with selectors

开发者 https://www.devze.com 2023-01-19 05:56 出处:网络
I have an unordered list with several divs contained inside. Each list item is created with jquery and put into a variable.

I have an unordered list with several divs contained inside. Each list item is created with jquery and put into a variable.

I need a way to select the :nth-child(even) of each div within the jquery object.

ele = myList.append("<li> \
<div class='name'>"+this.name+"</div> \
<div class='test'>test</div> \
</li>");

How would I use the variable "ele" within a jqu开发者_C百科ery object to get the nth-child contained within?

What I had imagined, which doesn't work:

$(ele+" div:nth-child(even)").addClass("my-class");

Another thing I imagined

ele.children("div:nth-child(even)").addClass('my-class'));

Also doesn't seem to work.


use .find()

ele.find("div:nth-child(even)").addClass('my-class');

crazy demo

0

精彩评论

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