📜  如何获取 dom js 的第 n 个孩子 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:34.040000             🧑  作者: Mango

代码示例1
//method one 
  var c = document.getElementById("myDIV").childNodes;
  c[1].style.backgroundColor = "yellow";//first child
  c[2].style.backgroundColor = "red";//second child
  c[-1].style.backgroundColor = "green";//last child
  c[-2].style.backgroundColor = "blue";//secound last child
//method two
 let nth-child = document.querySeletorAll('myDiv:nth-child(n)');//gives nth child