JavaScript操作XML/HTML比較常用的對象屬性集錦
節(jié)點對象屬性
childNodes—返回節(jié)點到子節(jié)點的節(jié)點列表
firstChild—返回節(jié)點的首個子節(jié)點。
lastChild—返回節(jié)點的最后一個子節(jié)點。
nextSibling—返回節(jié)點之后緊跟的同級節(jié)點。
nodeName—返回節(jié)點的名字,根據(jù)其類型。
nodeType—返回節(jié)點的類型。
nodeValue—設(shè)置或返回節(jié)點的值,根據(jù)其類型。
ownerDocument—返回節(jié)點的根元素(document對象)。
parentNode—返回節(jié)點的父節(jié)點。
previousSibling—返回節(jié)點之前緊跟的同級節(jié)點。
text—返回節(jié)點及其后代的文本(IE獨有)。
xml—返回節(jié)點及其后代的XML(IE獨有)。
節(jié)點對象的方法
appendChild()—向節(jié)點的子節(jié)點列表的結(jié)尾添加新的子節(jié)點。
cloneNode()—復(fù)制節(jié)點。
hasChildNodes()—判斷當(dāng)前節(jié)點是否擁有子節(jié)點。
insertBefore()—在指定的子節(jié)點前插入新的子節(jié)點。
normalize()—合并相鄰的Text節(jié)點并刪除空的Text節(jié)點。
removeChild()—刪除(并返回)當(dāng)前節(jié)點的指定子節(jié)點。
replaceChild()—用新節(jié)點替換一個子節(jié)點。
IE6獨有
selectNodes()—用一個XPath表達(dá)式查詢選擇節(jié)點。
selectSingleNode()—查找和XPath查詢匹配的一個節(jié)點。
transformNode()—使用XSLT把一個節(jié)點轉(zhuǎn)換為一個字符串。
transformNodeToObject()—使用XSLT把一個節(jié)點轉(zhuǎn)換成為一個文檔。
NodeList對象
length –返回節(jié)點列表中的節(jié)點數(shù)目。
item()—返回節(jié)點列表中處于指定的索引號的節(jié)點。
例如:
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—返回列表中節(jié)點數(shù)目。
getNamedItem()—返回指定的節(jié)點。(通過名稱)
item()—返回處于指定索引號的節(jié)點。
removeNamedItem()—刪除指定的節(jié)點(根據(jù)名稱)。
例如:
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—規(guī)定XML文件的下載是否應(yīng)當(dāng)被同步處理。
childNodes—返回屬于文檔的子節(jié)點的節(jié)點列表。
doctype—返回與文檔相關(guān)的文檔類型聲明。
documentElement—返回文檔的子節(jié)點。
firstChild—返回文檔的首個子節(jié)點。
implementation—返回處理該文檔的DOMImplementation對象。(IE沒有)
lastChild—返回文檔的最后一個子節(jié)點。
nodeType—返回節(jié)點類型。
nodeName—依據(jù)節(jié)點的類型返回其名稱。
nodeValue—依據(jù)節(jié)點的類型返回其值。
text—返回節(jié)點及其后代的文本(IE獨有)。
xml—返回節(jié)點及其后代的XML(IE獨有)。
Document對象的方法
createAttribute(att_name)—創(chuàng)建擁有指定名稱的屬性節(jié)點,并返回新的屬性對象。
createCDATASection(data)—創(chuàng)建CDATA區(qū)段節(jié)點。
createComment(data)—創(chuàng)建注釋節(jié)點。
createDocumentFragment—創(chuàng)建空的DocumentFragment對象,并返回此對象。
createElement(node_name)—創(chuàng)建元素節(jié)點。
createEntityReference(name)—創(chuàng)建EntityReference對象,并返回此對象。(IE獨有)
createTextNode(data)—創(chuàng)建文本節(jié)點。
getElementById(elementid)—查找具有指定的唯一ID的元素。
getElementsByTagName(node_name)—返回所有具有指定名稱的元素節(jié)點。
例如:
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—返回元素的子節(jié)點的NodeList
firstChild—返回元素的首個子節(jié)點。
lastChild—返回元素的最后一個子節(jié)點。
nextSibling—返回元素之后緊跟的節(jié)點。
nodeName—返回節(jié)點的名稱。
nodeType—返回元素的類型。
ownerDocument—返回元素所屬的根元素(document對象)。
parentNode—返回元素的父節(jié)點。
previousSibling—返回元素之前緊跟的節(jié)點。
tagName—返回元素的名稱。
text—返回節(jié)點及其后代的文本。(IE-only)
xml—返回節(jié)點及其后代得XML。(IE-only)
Element對象的方法
appendChild(node)—向節(jié)點的子節(jié)點列表末尾添加新的子節(jié)點。
cloneNode(true)—克隆節(jié)點。
getAttribute(att_name)—返回屬性的值。
getAttributeNode(att_name)—以 Attribute 對象返回屬性節(jié)點。
getElementsByTagName(node_name)—找到具有指定標(biāo)簽名的子孫元素。
hasAttribute(att_name)—返回元素是否擁有指定的屬性。
hasAttributes()—返回元素是否擁有屬性。
hasChildNodes()—返回元素是否擁有子節(jié)點。
insertBefore(new_node,existing_node)—在已有的子節(jié)點之前插入一新的子節(jié)點。
removeAttribute(att_name)—刪除指定的屬性。
removeAttributeNode(att_node)—刪除指定的屬性節(jié)點。
removeChild(node)—刪除子節(jié)點。
replaceChild(new_node,old_node)—替換子節(jié)點。
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—返回節(jié)點的名稱,依據(jù)其類型
nodeType—返回節(jié)點的類型。
nodeValue—設(shè)置或返回節(jié)點的值,依據(jù)其類型
ownerDocument—返回屬性所屬的根元素(document對象)。
specified—如果屬性值被設(shè)置在文檔中,則返回 true,如果其默認(rèn)值被設(shè)置在 DTD/Schema 中,則返回 false。
value—設(shè)置或返回屬性的值。
text—返回屬性的文本。IE-only。
xml—返回屬性的 XML。IE-only。
Text對象的屬性
data—設(shè)置或返回元素或?qū)傩缘奈谋尽?/p>
length—返回元素或?qū)傩缘奈谋鹃L度。
Text對象的方法
appendData(string)—向節(jié)點追加數(shù)據(jù)。
deleteData(start,length)—從節(jié)點刪除數(shù)據(jù)。
insertData(start,string)— 向節(jié)點中插入數(shù)據(jù)。
replaceData(start,length,string)—替換節(jié)點中的數(shù)據(jù)。
replaceData(offset)— 把一個 Text 節(jié)點分割成兩個。
substringData(start,length)— 從節(jié)點提取數(shù)據(jù)。
關(guān)于JavaScript操作XML/HTML比較常用的對象屬性集錦的全部敘述就到此結(jié)束了,更多內(nèi)容請登陸腳本之家官網(wǎng)了解更多,謝謝。
相關(guān)文章
Javascript實現(xiàn)hashcode函數(shù)實現(xiàn)對象比較與原理說明
在JavaScript中,數(shù)值的比較是比較簡單的,使用相等(==)和全等(===)符號基本上可以解決大多數(shù)非對象的比較。但是相等(==)和全等(===)符號在對象 object 的比較上,就不能滿足所有的要求了2023-06-06