<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script> window.onload = function(){ var oUl = document.getElementById("ul1"); //ie 6-8 高版本不支持 // oUl.firstChild.style.background = "red"; //高版本浏览器 // oUl.firstElementChild.style.background = "red"; //兼容写法 if(oUl.firstElementChild) { oUl.firstElementChild.style.background = "red"; }else{ oUl.firstChild.style.background = "red"; } } </script> <body> <ul id="ul1"> <li>1</li> <li>2</li> </ul> </body> </html>
lastChild lastElementChild nextSibling nextElementSibing previousSibling previousElementSibling 一样使用