Getting the 2nd child element

旧街凉风 提交于 2019-12-20 18:30:56

问题


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


回答1:


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



回答2:


check this link

Nth child selecter

Or you can try :eq Selector also

Eq selector




回答3:


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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/5440792/getting-the-2nd-child-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!