jQuery 获取第二个子元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5440792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the 2nd child element
提问by Carls Jr.
I am still new in jquery and I have this code
我还是 jquery 的新手,我有这个代码
<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.
我想得到第二个孩子,它们是使用 append 动态填充的,我不知道如何得到它。
I wanted to display
我想显示
$('the second element inner html here').dialog() etc..
$('the second element inner html here').dialog() etc..
Hoping someone can help me.
希望有人可以帮助我。
Thanks
谢谢
回答by issa marie tseng
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:
有多种方法可以做到这一点。我将假设顶级 div 的 id 为“top”。这可能是最好的一个:
$('#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
或者如果你知道至少有 2 个孩子
$($('#top').children()[1]).whatever();
回答by Harish
check this link
检查此链接
Or you can try :eq Selector also
或者你也可以试试 :eq Selector
回答by Ashwin Krishnamurthy
Use the nth-Child Selector.
For example: $('div:nth-child(2)')
使用nth-Child Selector。例如:$('div:nth-child(2)')
回答by sd1sd1
Maybe it sounds silly but $(".item").first().next()
does the trick.
也许这听起来很傻,但$(".item").first().next()
确实有效。
回答by newbie_86
What about giving the div an id and then just grabbing it by $('#mySecondDiv')
, depends how you dynamically generate it though...
给 div 一个 id 然后只是抓住它$('#mySecondDiv')
怎么样,取决于你如何动态生成它虽然......