按索引查找子项 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1526005/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:38:08  来源:igfitidea点击:

Find child by index jQuery

jquerydom

提问by sergionni

jQuery can return last or first child,it works ok.

jQuery 可以返回最后一个或第一个孩子,它工作正常。

But I need to get second child.

但我需要生第二个孩子。

This construction (get child by index) doesn't work,when get its text:

当获取其文本时,此构造(按索引获取子项)不起作用:

child.parent().parent().children().get(1).text()

So, how can i find non-last and non-first child (e.g. second)?

那么,我怎样才能找到非最后一个和非第一个孩子(例如第二个)?

回答by Tamas Czinege

Try this: (.eq()):

试试这个:(.eq()):

selection.eq(1).text()

回答by Lobstrosity

Try eq()instead of get():

尝试eq()代替get()

child.parent().parent().children().eq(1).text()

You can also do it by selector:

您也可以通过选择器来完成:

$("div:eq(1)")

回答by Florian Mertens

In one of my sites, I have:

在我的一个网站中,我有:

$('#tr_' + intID).find("td").eq(3).html("Hello there!");

Essentially, this will get all the TDelements from a table TRwith id='tr_123'.

从本质上讲,这将让所有TD从表中的元素TRid='tr_123'

eq(3)then gets the (0-indexed!) fourth cell of that TR, and changes its HTML contents to Hello there!.

eq(3)然后获取 ( 0-indexed!) 第四个单元格TR,并将其 HTML 内容更改为Hello there!.