js DOM 获取元素节点

2017-09-20  本文已影响0人  发光驴子
   <!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">
    <head>
    <style>
   </style>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>无标题文档</title>
    <script type="text/javascript">
    window.onload=function ()
    {
var oUl=document.getElementById('ul1');

//IE获取第一个节点
//oUl.firstChild.style.background='red';

//FF 火狐下获取第一个几点
//oUl.firstElementChild.style.background='red';

/*这是兼容的写法*/
var oFirst=oUl.firstElementChild||oUl.firstChild;
oFirst.style.background='red';
/*同理还有取最后一个节点
lastChild 是IE
lastElementChild 是火狐
获取兄弟节点
document.nextSibling       IE
document.nextElementSibling  火狐
document.previousSibling    IE
document.previousElementSibling 火狐
*/


    }
     </script>
    </head>

    <body>
   <ul id="ul1">
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
 </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读