AJAX入門---DOM操作HTML

來源:互聯網
上載者:User

標籤:dom   ajax   xml   標準   編程   

一邊學習AJAX一邊坐著總結加深印象,上次寫的是如何簡單的使用XMLHttpRequest對象,今天寫的是DOM(文件物件模型(Document Object Model))操作HTML,希望我的部落格對大家的學習能夠起到協助作用。         一.什麼是DOM

         檔案物件模型(Document Object Model,簡稱DOM),是W3C組織推薦的處理可擴充標誌語言的標準編程介面。DOM可以以一種獨立於平台和語言的方式訪問和修改一個文檔的內容和結構。換句話說,這是表示和處理一個HTML或XML文檔的常用方法。

         二.簡單的操作HTML

         今天的例子實現前我們先需要瞭解DOM的常用節點和常用API,例子中通過動態向HTML中添加元素節點達到學習的目的。

         最終效果

         添加節點實現

[plain] view plaincopyprint?
  1. //添加節點  
  2. var index = 0;  
  3. function appendnode() {  
  4.     //找到body對應節點  
  5.     var htmlNode = document.documentElement;  
  6.     var bodyNode = htmlNode.lastChild;  
  7.     //建立節點  
  8.     var divNode = document.createElement("div");  
  9.     var textNode = document.createTextNode("我是一個新加入的節點" +index++);  
  10.     //建立節點之間的關係  
  11.     divNode.appendChild(textNode);  
  12.     bodyNode.appendChild(divNode);  
  13. }  
//添加節點var index = 0;function appendnode() {    //找到body對應節點    var htmlNode = document.documentElement;    var bodyNode = htmlNode.lastChild;    //建立節點    var divNode = document.createElement("div");    var textNode = document.createTextNode("我是一個新加入的節點" +index++);    //建立節點之間的關係    divNode.appendChild(textNode);    bodyNode.appendChild(divNode);}

         插入節點實現

[plain] view plaincopyprint?
  1. //插入節點  
  2. function insertnode() {  
  3.     var removenode = document.getElementById("remove");  
  4.     var firstdivnode = removenode.nextSibling;  
  5.     while (firstdivnode) {  
  6.         if (firstdivnode&& firstdivnode.nodeName == "DIV") {  
  7.             var newnode =document.createElement("div");  
  8.             var textnode =document.createTextNode("我是一個新加入的節點" + index++);  
  9.             newnode.appendChild(textnode);  
  10.            document.documentElement.childNodes[2].insertBefore(newnode,firstdivnode);  
  11.             break;  
  12.         }  
  13.         firstdivnode =firstdivnode.nextSibling;  
  14.     }  
  15. }  
//插入節點function insertnode() {    var removenode = document.getElementById("remove");    var firstdivnode = removenode.nextSibling;    while (firstdivnode) {        if (firstdivnode&& firstdivnode.nodeName == "DIV") {            var newnode =document.createElement("div");            var textnode =document.createTextNode("我是一個新加入的節點" + index++);            newnode.appendChild(textnode);           document.documentElement.childNodes[2].insertBefore(newnode,firstdivnode);            break;        }        firstdivnode =firstdivnode.nextSibling;    }}

         移除節點實現

[plain] view plaincopyprint?
  1. function removenode() {  
  2.     //1.找到body  
  3.     //2.找到需要被移除的那個div  
  4.     //3.調用remove方法移除節點  
  5.    
  6.     var bodynode = document.getElementById("remove").parentNode;  
  7.     var removenode = document.getElementById("remove");  
  8.     var firstdivnode = removenode.nextSibling;  
  9.     while (firstdivnode) {  
  10.         if (firstdivnode&& firstdivnode.nodeName == "DIV") {  
  11.             bodynode.removeChild(firstdivnode);  
  12.             break;  
  13.         }  
  14.         firstdivnode = firstdivnode.nextSibling;  
  15.     }  
  16. }    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.