Html Dom 的nodetype解析

來源:互聯網
上載者:User

標籤:

Html Dom 的nodetype解析

將HTML DOM中幾個容易常用的屬性做下記錄:nodeName、nodeValue 以及 nodeType 包含有關於節點的資訊。

nodeName 屬性含有某個節點的名稱。

  • 元素節點的 nodeName 是標籤名稱
  • 屬性節點的 nodeName 是屬性名稱
  • 文本節點的 nodeName 永遠是 #text
  • 文檔節點的 nodeName 永遠是 #document

注釋:nodeName 所包含的 XML 元素的標籤名稱永遠是大寫的

nodeValue

對於文本節點,nodeValue 屬性包含文本。

對於屬性節點,nodeValue 屬性包含屬性值。

nodeValue 屬性對於文檔節點和元素節點是停用。

nodeType

nodeType 屬性可返回節點的類型。

最重要的節點類型是:

元素類型 節點類型
元素element 1
屬性attr 2
文本text 3
注釋comments 8
文檔document 9
HTML檔案:<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>DOM標準</title> <script type="text/javascript" src="test.js"></js> </head> <body> <h1 id="h1">An HTML Document</h1> <p id="p1">This is a <i>W3C HTML DOM</i> document.</p> <p><input id="btnDemo1" type="button" value="取H1 Element節點值"></p> <p><input id="btnDemo2" type="button" value="取H1 Element節點文本"></p> <p><input id="btnDemo3" type="button" value="取Document Element節點文本"></p> <p><input type="button" alt="這是個示範按鈕" title="示範按鈕提示標題" name="btnShowAttr" id="btnShowAttr" value="按鈕節點示範" /></p> </body> </html>JS:function showElement(){ var element=document.getElementById("h1");//h1是一個<h1>標籤 alert(‘nodetype:‘+element.nodeType);//nodeType=1 alert(‘nodeName:‘+element.nodeName); alert(‘nodeValue:‘+element.nodeValue); //null alert(‘element:‘+element);   }
function showText(){ var element=document.getElementById("h1"); var text=element.childNodes[0]; alert(‘nodeType:‘+text.nodeType);   //nodeType=3 alert(‘nodeValue:‘+text.nodeValue);   //文本節點的nodeValue是其常值內容 text.nodeValue=text.nodeValue+"abc"; //常值內容添加修改刪除等等。 alert(‘nodeName:‘+text.nodeName); alert(text.data);    //data同樣是其內容,這個屬性下同樣可以增刪改。 }
function showDocument(){ alert(‘nodeType:‘+document.nodeType);   //9 alert(‘nodeName:‘+document.nodeName); alert(document); }
function showAttr(){ var btnShowAttr=document.getElementById("btnShowAttr"); //示範按鈕,有很多屬性 var attrs=btnShowAttr.attributes; for(var i=0;i<attrs.length ;i++){    var attr=attrs[i];   alert(‘nodeType:‘+attr.nodeType); //attribute 的nodeType=2   alert(‘attr:‘+attr);    alert(‘attr.name:‘+attr.name+‘=‘+attr.value);   }
}
function demo(){ var btnDemo1=document.getElementById("btnDemo1"); btnDemo1.onclick=showElement;  //按鈕1取節點nodetype值 var btnDemo2=document.getElementById("btnDemo2"); btnDemo2.onclick=showText; var btnDemo3=document.getElementById("btnDemo3"); btnDemo3.onclick=showDocument; var btnShowAttr=document.getElementById("btnShowAttr"); btnShowAttr.onclick=showAttr;
} window.onload=demo;

Html Dom 的nodetype解析

聯繫我們

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