开发者

Accessing jquery objects with selectors

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜