妙味——DOM基础2

首尾子节点:

  有兼容性问题

  firstChild、firstElementChild

  lastChild、lastElementChild

兄弟节点:

  有兼容性问题

  nextSibling、nextElementSibling

  previousSibling、previousElementSibling

例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script>
window.onload=function(){
    var oUl = document.getElementById('ul1');

    //IE7 IE8
    // oUl.firstChild.style.background='red';    

    //IE9+ FF chrome
    // oUl.firstElementChild.style.background='red';

    //处理方法
    var oFirst = oUl.firstElementChild||oUl.firstChild;

    oFirst.style.background='red';
}
</script>
</head>
<body>
    <ul id="ul1">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>
高否?富否?帅否? 否? 滚去学习!
原文地址:https://www.cnblogs.com/baixc/p/3458364.html