JS document练习

来源:互联网 发布:软件测试未来发展方向 编辑:程序博客网 时间:2024/05/04 13:05

<!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>
    <title></title>
    <script type="text/javascript">
        function shownode() {

            var ohtml = document.documentElement;
            //alert(ohtml.nodeName);
            //alert(ohtml.childNodes.length);
            //alert(ohtml.childNodes[1].nodeName); //获取body
            //var obody = ohtml.childNodes[1];//如果你愿意癮一层一层一层剥开ahtml的心,就可以找到div
            var obody = ohtml.lastChild; //快速获取ohtml的最后一个子节点?
            //alert(obody.childNodes[0].nodeName);
            //alert(obody.parentNode.nodeName);//根据子节点找到想要的父节点?
            alert(obody.previousSibling.nodeName);

            var ohead = ohtml.childNodes[0];
            alert(ohead.nextSibling.nodeName);

        }
    </script>
   
</head>
<body>
<div id="div1">第一层</div>
<div id="div2">
        第二层</div>
    <div>
        第三层
        <img src="images/4.jpg" />
    </div>
    <div>
        第四层<input id="Button1" type="button" value="显示节点"  onclick="shownode();"/></div>


</body>
</html>

原创粉丝点击