我有以下层次结构:
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;
为了在变量中使用第一个孩子或第 n 个孩子,我需要做什么?