获得第二个子元素

Car*_*Jr. 16 html jquery jquery-selectors

我仍然是jquery的新手,我有这个代码

<div>
   abcsdf
   <div> first child</div>
   <div> second child</div>
</div>
Run Code Online (Sandbox Code Playgroud)

我想得到第二个孩子,他们使用追加动态填充,我不知道如何得到它.

我想要展示

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

希望有人可以帮助我.

谢谢

iss*_*eng 35

有很多方法可以做到这一点.我将假设顶级div的id为'top'.这可能是最好的一个:

$('#top > :nth-child(2)').whatever();
Run Code Online (Sandbox Code Playgroud)

要么

$('#top').children(':first-child').next().whatever();
Run Code Online (Sandbox Code Playgroud)

或者如果你知道一个事实,至少有2个孩子

$($('#top').children()[1]).whatever();
Run Code Online (Sandbox Code Playgroud)

  • whatever()是超级秘密的功能 (6认同)

Har*_*ish 8

检查此链接

第N个孩子选择

或者您可以尝试:eq Selector也

Eq选择器


Ash*_*thy 7

使用第n个子选择器.例如:$('div:nth-child(2)')