DOM基础教程之使用DOM(2)


    <script language="javascript">
       window.onload = function(){
     var food = document.getElementById("mydearfood");
      document.write(food.parentNode.tagName)
        }
    </script>
</head>
<body>
<ul>
    <li>糖醋排骨</li>
    <li>圆笼粉蒸肉</li>
    <li>泡菜鱼</li>
    <li>板栗烧鸡</li>
    <li>麻婆豆腐</li>
</ul>
//返回 ul

使用父节点,成功的获得了指定节点的父节点

5.使用parentNode属性

复制代码 代码如下:


<head>
    <title>childNodes</title>
    <script language="javascript">
       window.onload = function(){
     var food = document.getElementById("mydearfood");
           var parentElm = food.parentNode;
           while(parentElm.className != "colorful" && parentElm != document.body)
               parentElm = parentElm.parentNode;    //一路往上找
           document.write("tageName:"+parentElm.tagName+"<br>");
           document.write("claaName:"+parentElm.className+"<br>");
           document.write("typeOf:"+typeof(food)+"<br>");
        }
    </script>
</head>
<body>
<div>
<ul>
    <span>
    <li>糖醋排骨</li>
    <li>圆笼粉蒸肉</li>
    <li>泡菜鱼</li>
    <li>板栗烧鸡</li>
    <li>麻婆豆腐</li>
    </span>
</ul>
    </div>
</body><br>//输出<br>//tageName:DIV<br>claaName:colorful<br>typeOf:object

  从某个子节点开始,一直向上搜索父节点,直到节点的类名为“colorful”

6.dom的兄弟关系

复制代码 代码如下:


<head>
    <title>childNodes</title>
    <script language="javascript">
       window.onload = function(){
     var foods = document.getElementById("mydearfood");
     var nextF = foods.nextSibling;
     alert("nextSibling:"+nextF.tagName +"<br>");
        }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxypd.html