javascript DOM(四)----節點替換

來源:互聯網
上載者:User

節點的替換:
 1). replaceChild(): 把一個給定父元素裡的一個子節點替換為另外一個子節點
   var reference = element.replaceChild(newChild,oldChild);
   傳回值是一個指向已被替換的那個子節點的引用指標
 2). 該節點除了替換功能以外還有移動的功能. 
 3). 該方法只能完成單向替換, 若需要使用雙向替換, 需要自訂函數:
 /**
  * 互換 aNode 和 bNode
  * @param {Object} aNode
  * @param {Object} bNode
  */
 function replaceEach(aNode, bNode){
  
  if(aNode == bNode){
   return;
  }
  
  var aParentNode = aNode.parentNode;
  //若 aNode 有父節點
  if(aParentNode){
   var bParentNode = bNode.parentNode;
   
   //若 bNode 有父節點 
   if(bParentNode){
    var tempNode = aNode.cloneNode(true);
    bParentNode.replaceChild(tempNode, bNode);
    aParentNode.replaceChild(bNode, aNode); 
   }
  }
 
 }  
Html代碼 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>Untitled Document</title> 
        <script type="text/javascript"> 
             
            window.onload = function(){ 
                //1. 把 "東京" 節點替換為 "平壤" 
                var pr = document.createElement("li"); 
                pr.appendChild(document.createTextNode("平壤")); 
                 
                document.getElementById("city").replaceChild(pr, document.getElementById("dj")); 
                 
                //2. 實現 #bj 和 #rl 之間的互換, 需要使用 cloneNode() 方法 
                var bj = document.getElementById("bj"); 
                var rl = document.getElementById("rl"); 
//               
//              var city = document.getElementById("city"); 
//              var game = document.getElementById("game"); 
//               
//              city.replaceChild(rl.cloneNode(true), bj); 
//              game.replaceChild(bj, rl); 
                replaceEach(rl, bj); 
            }; 
             
            /** 
             * 互換節點 
             * @param {Object} aNode 
             * @param {Object} bNode 
             */ 
            function replaceEach(aNode, bNode){ 
                var aParentNode = aNode.parentNode; 
                var bParentNode = bNode.parentNode; 
                 
                //若 aNode 和 bNode 都存在父節點 
                if(aParentNode && bParentNode){ 
                    aParentNode.replaceChild(bNode.cloneNode(true), aNode); 
                    bParentNode.replaceChild(aNode, bNode); 
                } 
            } 
                         
        </script> 
         
    </head> 
    <body> 
        <p>你喜歡哪個城市?</p> 
        <ul id="city"> 
            <li id="bj" name="BeiJing">北京</li> 
            <li>上海</li> 
            <li id="dj">東京</li> 
            <li>首爾</li> 
        </ul> 
         
        <br><br> 
        <p>你喜歡哪款單機遊戲?</p> 
        <ul id="game"> 
            <li id="rl">紅警</li> 
            <li>實況</li> 
            <li>極品飛車</li> 
            <li>魔獸</li> 
        </ul> 
         
        <br><br> 
        gender:  
            <input type="radio" name="gender" value="male"/>Male 
            <input type="radio" name="gender" value="female"/>Female 
    </body> 
</html>    

 

本文出自“change”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.