dom节点相关问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>dom节点</title>
<script type="text/javascript">
window.onload = function(){
    var oUl = document.getElementsByTagName('ul')[0];
    // alert(oUl.childNodes.length)    // 7
    // alert(oUl.children.length)// 3  children 实际上是指的元素节点
    var oLi = document.getElementById('two');
    alert(oLi.parentNode)  
    // nodeType 1是指元素节点  3是指文本节点;
    alert(oLi.parentNode.nodeType) //1
    alert(oUl.firstChild.nodeType) //3
    alert(oLi.nextSibling.nodeType)// 3
    alert(oLi.nextElementSibling.nodeType )//  1 不兼容的情况下可以试试nextElementSibling.nodeType,一般情况下是可以的。  
    
 }
</script>
</head>
<body>
    <ul>
        <li>111111</li>
        <li id="two">46343</li>
        <li>3333</li>
    </ul>
</body>
</html>

posted @ 2019-04-17 15:39  代码,让世界更美好!  阅读(122)  评论(0编辑  收藏  举报