Getting the 2nd child element

2019-03-18 01:32发布

I am still new in jquery and I have this code

<div>
   abcsdf
   <div> first child</div>
   <div> second child</div>
</div>

I wanted to get the second child, they are dynamically populated using append and I don't know how to get it.

I wanted to display

$('the second element inner html here').dialog() etc..

Hoping someone can help me.

Thanks

5条回答
趁早两清
2楼-- · 2019-03-18 02:06

check this link

Nth child selecter

Or you can try :eq Selector also

Eq selector

查看更多
\"骚年 ilove
3楼-- · 2019-03-18 02:06

Use the nth-Child Selector. For example: $('div:nth-child(2)')

查看更多
可以哭但决不认输i
4楼-- · 2019-03-18 02:09

Maybe it sounds silly but $(".item").first().next() does the trick.

查看更多
乱世女痞
5楼-- · 2019-03-18 02:29

A number of ways to do this one. I'm going to assume the toplevel div has an id of 'top'. This is probably the best one:

$('#top > :nth-child(2)').whatever();

or

$('#top').children(':first-child').next().whatever();

or if you know for a fact there are at least 2 children

$($('#top').children()[1]).whatever();
查看更多
你好瞎i
6楼-- · 2019-03-18 02:29

What about giving the div an id and then just grabbing it by $('#mySecondDiv'), depends how you dynamically generate it though...

查看更多
登录 后发表回答