选择第n个孩子 - children vs childNodes属性

时间:2014-03-09 22:49:10

标签: javascript

当你想选择第n个孩子时哪个更好或有什么区别?请注意我不想使用jQuery。

1 个答案:

答案 0 :(得分:6)

嗯,你没有提供太多信息,但你有两种方法:

node成为DOM节点:

node.childNodes[1]; //0 is the first child.

另一种方式......

node.querySelector(':nth-child(2)'); //in selectors, the first child has index 1.