JavaScript操作XML/HTML比較常用的對象屬性集錦_javascript技巧

來源:互聯網
上載者:User

節點對象屬性

childNodes—返回節點到子節點的節點列表

firstChild—返回節點的首個子節點。
lastChild—返回節點的最後一個子節點。

nextSibling—返回節點之後緊跟的同級節點。

nodeName—返回節點的名字,根據其類型。
nodeType—返回節點的類型。
nodeValue—設定或返回節點的值,根據其類型。

ownerDocument—返回節點的根項目(document對象)。

parentNode—返回節點的父節點。

previousSibling—返回節點之前緊跟的同級節點。

text—返回節點及其後代的文本(IE專屬)。

xml—返回節點及其後代的XML(IE專屬)。

節點對象的方法

appendChild()—向節點的子節點列表的結尾添加新的子節點。

cloneNode()—複製節點。

hasChildNodes()—判斷當前節點是否擁有子節點。

insertBefore()—在指定的子節點前插入新的子節點。

normalize()—合并相鄰的Text節點並刪除空的Text節點。

removeChild()—刪除(並返回)當前節點的指定子節點。

replaceChild()—用新節點替換一個子節點。

IE6專屬

selectNodes()—用一個XPath運算式查詢選擇節點。

selectSingleNode()—尋找和XPath查詢匹配的一個節點。

transformNode()—使用XSLT把一個節點轉換為一個字串。

transformNodeToObject()—使用XSLT把一個節點轉換成為一個文檔。

NodeList對象

length –返回節點列表中的節點數目。

item()—返回節點列表中處於指定的索引號的節點。

例如:

Javascript代碼

xmlDoc = loadXMLDoc(“books.xml”); var x = xmlDoc.getElementsByTagName(“title”); document.write(“title element:” + x.length); 

輸出:title element:4

Javascript代碼

var y = xmlDoc.documentElement.childNodes; document.write(y.item(0).nodeName); 

輸出:book

NamedNodeMap對象

length—返回列表中節點數目。

getNamedItem()—返回指定的節點。(通過名稱)

item()—返回處於指定索引號的節點。

removeNamedItem()—刪除指定的節點(根據名稱)。

例如:

Javascript代碼

xmlDoc = loadXMLDoc(“books.xml”); var x = xmlDoc.getElementsByTagName(“book”); document.write(x.item(0).attributes.length); 

輸出:1

Javascript代碼

document.write(x.item(0).attributes.getNamedItem(“category”); 

輸出:COOKING 

Javascript代碼 

x.item(0).attributes.removeNamedItem(“category”); 

刪除第一個book元素的category屬性

Document對象代表整個XML文檔。

Document對象的屬性。

async—規定XML檔案的下載是否應當被同步處理。

childNodes—返回屬於文檔的子節點的節點列表。

doctype—返回與文檔相關的文件類型聲明。

documentElement—返迴文檔的子節點。

firstChild—返迴文檔的首個子節點。

implementation—返回處理該文檔的DOMImplementation對象。(IE沒有)

lastChild—返迴文檔的最後一個子節點。

nodeType—返回節點類型。

nodeName—依據節點的類型返回其名稱。

nodeValue—依據節點的類型返回其值。

text—返回節點及其後代的文本(IE專屬)。

xml—返回節點及其後代的XML(IE專屬)。

Document對象的方法

createAttribute(att_name)—建立擁有指定名稱的屬性節點,並返回新的屬性對象。
createCDATASection(data)—建立CDATA區段節點。
createComment(data)—建立注釋節點。
createDocumentFragment—建立空的DocumentFragment對象,並返回此對象。
createElement(node_name)—建立元素節點。
createEntityReference(name)—建立EntityReference對象,並返回此對象。(IE專屬)
createTextNode(data)—建立文本節點。
getElementById(elementid)—尋找具有指定的唯一ID的元素。
getElementsByTagName(node_name)—返回所有具有指定名稱的元素節點。

例如:

Javascript代碼 

var xmlDoc = loadXMLDoc("book.xml");  xmlDoc.async = false; var book = xmlDoc.getElementsByTagName("book"); var newtext1="Special Offer & Book Sale"; var newCDATA=xmlDoc.createCDATASection(newtext1); book[0].appendChild(newCDATA); var newtext2="Revised September 2006"; var newComment=xmlDoc.createComment(newtext2); book[0].appendChild(newComment); var var newel=xmlDoc.createElement('edition'); var newtext3=xmlDoc.createTextNode('First'); newel.appendChild(newtext3); book[0].appendChild(newel); document.write("<xmp>" + xmlDoc.xml + "</xmp>"); 

Element 對象的屬性
attributes—返回元素的屬性的NamedNodeMap
childNodes—返回元素的子節點的NodeList
firstChild—返回元素的首個子節點。
lastChild—返回元素的最後一個子節點。
nextSibling—返回元素之後緊跟的節點。
nodeName—返回節點的名稱。
nodeType—返回元素的類型。
ownerDocument—返回元素所屬的根項目(document對象)。
parentNode—返回元素的父節點。
previousSibling—返回元素之前緊跟的節點。
tagName—返回元素的名稱。
text—返回節點及其後代的文本。(IE-only)
xml—返回節點及其後代得XML。(IE-only)

Element對象的方法

appendChild(node)—向節點的子節點列表末尾添加新的子節點。
cloneNode(true)—複製節點。
getAttribute(att_name)—返回屬性的值。
getAttributeNode(att_name)—以 Attribute 對象返回屬性節點。
getElementsByTagName(node_name)—找到具有指定標籤名的子孫元素。
hasAttribute(att_name)—返回元素是否擁有指定的屬性。
hasAttributes()—返回元素是否擁有屬性。
hasChildNodes()—返回元素是否擁有子節點。
insertBefore(new_node,existing_node)—在已有的子節點之前插入一新的子節點。
removeAttribute(att_name)—刪除指定的屬性。
removeAttributeNode(att_node)—刪除指定的屬性節點。
removeChild(node)—刪除子節點。
replaceChild(new_node,old_node)—替換子節點。
setAttribute(name,value)—添加新的屬性或者改變屬性的值。
setAttribute(att_node)—添加新的屬性。

Javascript代碼

x=xmlDoc.getElementsByTagName('book'); for(i=0;i<x.length;i++) { attnode=x.item(i).getAttributeNode("category"); document.write(attnode.name); document.write(" = "); document.write(attnode.value); document.write("<br />"); } for(i=0;i<x.length;i++){ document.write(x[i].getAttribute('category')); document.write("<br />"); } xmlDoc=loadXMLDoc("/example/xdom/books.xml"); x=xmlDoc.getElementsByTagName('book'); document.write(x[0].getAttribute('category')); document.write("<br />"); x[0].removeAttribute('category'); document.write(x[0].getAttribute('category')); var attnode = x[1].getAttributeNode("category"); var y = x[1].removeAttributeNode(attnode); document.write("<xmp>" + xmlDoc.xml + "</xmp>"); function get_lastchild(n) {  x = n.lastChild;  while(x.noteType!=1){   x = x.previousSibling;  }  return x; } function get_firstChild(n){  x = n.firstChild;  whild(x.nodeType!=1){  x=x.nextSibling;  }  return x; } xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; deleted_node=x.removeChild(get_lastchild(x)); document.write("Node removed: " + deleted_node.nodeName); 

Attr對象

Attr 對象表示 Element 對象的屬性。

name—返回屬性的名稱。

nodeName—返回節點的名稱,依據其類型

nodeType—返回節點的類型。

nodeValue—設定或返回節點的值,依據其類型

ownerDocument—返回屬性所屬的根項目(document對象)。

specified—如果屬性值被設定在文檔中,則返回 true,如果其預設值被設定在 DTD/Schema 中,則返回 false。

value—設定或返回屬性的值。

text—返回屬性的文本。IE-only。

xml—返回屬性的 XML。IE-only。

Text對象的屬性

data—設定或返回元素或屬性的文本。

length—返回元素或屬性的文本長度。

Text對象的方法

appendData(string)—向節點追加資料。

deleteData(start,length)—從節點刪除資料。

insertData(start,string)— 向節點中插入資料。

replaceData(start,length,string)—替換節點中的資料。

replaceData(offset)— 把一個 Text 節點分割成兩個。

substringData(start,length)— 從節點提取資料。

關於JavaScript操作XML/HTML比較常用的對象屬性集錦的全部敘述就到此結束了,更多內容請登陸雲棲社區官網瞭解更多,謝謝。

聯繫我們

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