av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

JavaScriptDOM修改文檔樹方法實(shí)例

在我們工作中大多數(shù)DOM腳本的主要任務(wù)就是在DOM文檔中插入,刪除和移動節(jié)點(diǎn)。W3C DOM 提供了4種方法來修改文檔樹。常用的是appendChild()和insertBefore(),而removeChild() 和replaceChild()很少用到。

一般用法

修改文檔提供的4個(gè)方法,都是指向它們所作用的節(jié)點(diǎn)的引用。導(dǎo)航條實(shí)例:

 
 
 
  1.  
  2.     

    我的導(dǎo)航條

                
  3.      
  4.        
  5. HOME
  6.  
  7.        
  8. (X)Html / Css
  9.  
  10.        
  11. Ajax / RIA
  12.  
  13.        
  14. GoF
  15.  
  16.        
  17. JavaScript
  18.  
  19.        
  20. JavaWeb
  21.  
  22.        
  23. jQuery
  24.  
  25.        
  26. MooTools
  27.  
  28.        
  29. Python
  30.  
  31.        
  32. Resources
  33.  
  34.      
  

appendChild()

appendChild()方法讓你添加一個(gè)節(jié)點(diǎn)并使其成為某個(gè)元素的***一個(gè)子節(jié)點(diǎn)。如果添加的該節(jié)點(diǎn)已經(jīng)存在于文檔中,它會從當(dāng)前位置移除。該節(jié)點(diǎn)的子節(jié)點(diǎn)保持不變,它們也被一起移動到新的位置。

 
 
 
  1.  
  2. window.onload=function(){  
  3.   /*為一個(gè)元素添加子元素*/  
  4.   var nav=document.getElementById("nav");  
  5.   //創(chuàng)建一個(gè)li新元素  
  6.   var newChild=document.createElement('li');  
  7.   //創(chuàng)建一個(gè)a 新元素  
  8.   var newLink=document.createElement('a')  
  9.   //創(chuàng)建一個(gè) Text 節(jié)點(diǎn)  
  10.   var newText=document.createTextNode('My Wiki');  
  11.   //把Text添加到a元素節(jié)點(diǎn)中  
  12.   newLink.appendChild(newText);  
  13.   //給a元素節(jié)點(diǎn)設(shè)置屬性href和內(nèi)容  
  14.   newLink.setAttribute('href',"#");  
  15.   //把a(bǔ)元素節(jié)點(diǎn)添加到新的li元素節(jié)點(diǎn)中  
  16.   newChild.appendChild(newLink);  
  17.   //把新的li元素節(jié)點(diǎn)添加到 ul 元素節(jié)點(diǎn)里  
  18.   nav.appendChild(newChild);  
  19.    
  20.   /*
  21. 從原始位置上被移除,成為ul的***一個(gè)子節(jié)點(diǎn)。它的a 元素節(jié)點(diǎn)和文本節(jié)點(diǎn)HODE也被移了過來*/  
  22.   navnav_list=nav.getElementsByTagName("li");//返回相同的一組元素  
  23.   nav.appendChild(nav_list[0]);  
  24. }  
  25.   

創(chuàng)建新DOM元素的通用函數(shù)

 
 
 
  1.  
  2. function create( elem ) {  
  3.     return document.createElementNS ?  
  4.         document.createElementNS( 'http://www.w3.org/1999/xhtml', elem ) :  
  5.         document.createElement( elem );  
  6. }  
  7.    
  8.   

我們看到結(jié)果:

添加好的子節(jié)點(diǎn)

從原始位置上被移除,成為ul的***一個(gè)子節(jié)點(diǎn)

insertBefore()

insertBefore()方法允許你在其他節(jié)點(diǎn)的前面插入一個(gè)節(jié)點(diǎn),所以當(dāng)你想要添加一個(gè)子節(jié)點(diǎn),但又不希望該節(jié)點(diǎn)成為***一個(gè)子節(jié)點(diǎn)的時(shí)候,就可以使用此方法。就像appendChild()方法一樣,如果插入的節(jié)點(diǎn)已經(jīng)存在于文檔之中,它會被從當(dāng)前位置移除,而且該節(jié)點(diǎn)在被插入之后乃保持它的子節(jié)點(diǎn)結(jié)構(gòu)。

 
 
 
  1.  
  2. window.onload=function(){  
  3.   var nav=document.getElementById("nav");  
  4.   navnav_list=nav.getElementsByTagName("li");//返回相同的一組元素  
  5.   //***個(gè)節(jié)點(diǎn)  
  6.   var x=nav_list[0];  
  7.   //***一個(gè)節(jié)點(diǎn)  
  8.   var y=nav_list[nav_list.length-1]  
  9.   //在x元素對象之前插入y元素對象  
  10.   nav.insertBefore(y,x);  
  11.   //在x元素對象之前插入新生產(chǎn)的newChild元素對象  
  12.   nav.insertBefore(newChild,x);//newChild元素對象的創(chuàng)建請參考上面的代碼  
  13. }  
  14.   

 #p#

在其他節(jié)點(diǎn)的前面插入一個(gè)節(jié)點(diǎn)

replaceChild()

replaceChild()方法允許你把一個(gè)節(jié)點(diǎn)替換為另一個(gè)節(jié)點(diǎn)。就像appendChild()和insertBefore()一樣,如果插入的節(jié)點(diǎn)已經(jīng)存在于文檔之中,它會被從當(dāng)前位置移除,而且該節(jié)點(diǎn)在被插入之后乃保持它的子節(jié)點(diǎn)結(jié)構(gòu)。

 
 
 
  1.  
  2. window.onload=function(){  
  3.   var nav=document.getElementById("nav");  
  4.   navnav_list=nav.getElementsByTagName("li");//返回相同的一組元素  
  5.   //***個(gè)節(jié)點(diǎn)對象  
  6.   var x=nav_list[0];  
  7.   //x節(jié)點(diǎn)對象被newChild新節(jié)點(diǎn)對象替換了  
  8.   nav.replaceChild(newChild,x);  
  9. }  
  10.   

把一個(gè)節(jié)點(diǎn)替換為另一個(gè)節(jié)點(diǎn)

removeChild()

removeChild()方法允許你移除一個(gè)節(jié)點(diǎn)以及它的子節(jié)點(diǎn)們。

 
 
 
  1.  
  2. window.onload=function(){  
  3.   var nav=document.getElementById("nav");  
  4.   navnav_list=nav.getElementsByTagName("li");//返回相同的一組元素  
  5.   //***一個(gè)節(jié)點(diǎn)  
  6.   var y=nav_list[nav_list.length-1]  
  7.   //移除***面的一個(gè)節(jié)點(diǎn)  
  8.   nav.removeChild(y);  
  9. }  
  10.   

你移除一個(gè)節(jié)點(diǎn)以及它的子節(jié)點(diǎn)們

移除所有的子節(jié)點(diǎn)

有的時(shí)候你需要把一個(gè)元素清除干凈;你想在添加新節(jié)點(diǎn)前清除原來的所有子點(diǎn)。有兩個(gè)簡單的方法來做這件事情:

 
 
 
  1.  
  2.  while (x.childNodes[0]){  
  3.   x.removeChild(x.childNodes[0]);  
  4.  }  
  5.    
  6.  /*  
  7.  //我們可以使用firstChild來代替childNodes[0]  
  8.   while (x.firstChild){  
  9.   x.removeChild(x.firstChild);  
  10.  }  
  11.  * /  
  12.   

這是一個(gè)簡單的while()循環(huán),只要元素存在***個(gè)節(jié)點(diǎn)(childNodes[0]),它就移除這個(gè)節(jié)點(diǎn),接著節(jié)點(diǎn)集合立即更新。所以(原來的)第二個(gè)節(jié)點(diǎn)成為了***個(gè)節(jié)點(diǎn),循環(huán)就會一直重復(fù),直到X沒有子節(jié)點(diǎn)為止。另一個(gè)方法就簡單了

 
 
 
  1.  
  2. x.innerHTML='';  
  3.   

 #p#

輔助函數(shù)

appendChild()和insertBefore()都有2個(gè)參數(shù),但是我們在應(yīng)用的時(shí)候,還要注意參數(shù)的先后順序。既然這么麻煩我們還是自己寫一些輔助函數(shù)來代替原有的appendChild()和insertBefore()。在另一個(gè)元素之前插入元素的函數(shù):

 
 
 
  1.  
  2. //insertBefore()的代替方法  
  3. function before( parent, before, elem ) {  
  4.     // Check to see if no parent node was provided  
  5.  //檢查parent是否傳入  
  6.     if ( elem == null ) {  
  7.         elem = before;  
  8.         before = parent;  
  9.         parent  = before.parentNode;  
  10.     }  
  11.    
  12.     // Get the new array of elements  
  13.  //獲取元素的新數(shù)組  
  14.     var elems = checkElem( elem );  
  15.    
  16.     // Move through the array backwards,  
  17.     // because we’re prepending elements  
  18.  //向后遍歷數(shù)組  
  19.  //因?yàn)槲覀兿蚯安迦朐? 
  20.     for ( var i = elems.length - 1; i >= 0; i-- ) {  
  21.         parent.insertBefore( elems[i], before );  
  22.     }  
  23. }  
  24.   

為另一個(gè)元素添加一個(gè)子元素:

 
 
 
  1.  
  2. //appendChild()的代替方法  
  3. function append( parent, elem ) {  
  4.     // Get the array of elements  
  5.  //獲取元素?cái)?shù)組  
  6.     var elems = checkElem( elem );  
  7.    
  8.     // Append them all to the element  
  9.  //把它們所有都追加到元素中  
  10.     for ( var i = 0; i <= elems.length; i++ ) {  
  11.         parent.appendChild( elems[i] );  
  12.     }  
  13. }  
  14.   

before和append的輔助函數(shù):

 
 
 
  1.  
  2. function checkElem( elem ) {  
  3.     // If only a string was provided, convert it into a Text Node  
  4.  //如果只提供字符串,那就把它轉(zhuǎn)換為文本節(jié)點(diǎn)  
  5.     return elem && elem.constructor == String ?  
  6.    document.createTextNode( elem ) : elem;  
  7. }  
  8.   

注意:constructor的用法。

有時(shí)你可能需要對變量進(jìn)行類型檢查,或者判斷變量是否已定義。有兩種方法可以使用:typeof函數(shù)與constructor屬性。typeof可以檢查到變量是否有定義,而construct只能檢查已定義變量的類型。

移除所有的子節(jié)點(diǎn)的輔助函數(shù):

 
 
 
  1.   
  2. function empty( elem ) {   
  3.  while (elem.firstChild){  
  4.    remove(elem.firstChild);  
  5.  }  
  6.  /*  
  7.  //我們可以使用firstChild來代替childNodes[0]  
  8.  while (elem.childNodes[0])  
  9.   remove(elem.childNodes[0]);  
  10.  * /  
  11. }  
  12.    
  13. function remove( elem ) {  
  14.     if ( elem ) elem.parentNode.removeChild( elem );  
  15. }  
  16.  

當(dāng)前標(biāo)題:JavaScriptDOM修改文檔樹方法實(shí)例
瀏覽路徑:http://uogjgqi.cn/article/coodjip.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流