我有以下等级:
menu > li:first-child
menu > li:nth-child(2)
menu > li:nth-child(3)
menu > li:nth-child(4)
menu > li:nth-child(5)
menu > li:nth-child(6)
menu > li:nth-child(7)
menu > li:nth-child(8)
menu > li:nth-child(9)
我试图将其中的每一个放入变量中以便:
var fromleft = box.getBoundingClientRect().left;
var fromtop = box.getBoundingClientRect().top;
现在可以使用:
var box = document.getElementById('menu');
var fromleft = box.getBoundingClientRect().left;
var fromtop = box.getBoundingClientRect().top;
但不是:
var box = document.getElementById('menu > li:first-child');
var fromleft = box.getBoundingClientRect().left;
var fromtop = box.getBoundingClientRect().top;
为了在变量中使用first-child或nth-child,我需要做什么?
答案 0 :(得分:0)
我遇到了这个,我认为这就是我需要的东西!
var box = document.getElementById( 'sdt_menu' ).getElementsByTagName( 'li' )[0];"
并且... 0 =第一个孩子,1 =第n个孩子(2),等等
另外......' li'可以更改为' div'和其他人。
(或者看起来好像!)