JavaScript操作XML/HTML比較常用的對(duì)象屬性集錦
節(jié)點(diǎn)對(duì)象屬性
childNodes—返回節(jié)點(diǎn)到子節(jié)點(diǎn)的節(jié)點(diǎn)列表
firstChild—返回節(jié)點(diǎn)的首個(gè)子節(jié)點(diǎn)。
lastChild—返回節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)。
nextSibling—返回節(jié)點(diǎn)之后緊跟的同級(jí)節(jié)點(diǎn)。
nodeName—返回節(jié)點(diǎn)的名字,根據(jù)其類型。
nodeType—返回節(jié)點(diǎn)的類型。
nodeValue—設(shè)置或返回節(jié)點(diǎn)的值,根據(jù)其類型。
ownerDocument—返回節(jié)點(diǎn)的根元素(document對(duì)象)。
parentNode—返回節(jié)點(diǎn)的父節(jié)點(diǎn)。
previousSibling—返回節(jié)點(diǎn)之前緊跟的同級(jí)節(jié)點(diǎn)。
text—返回節(jié)點(diǎn)及其后代的文本(IE獨(dú)有)。
xml—返回節(jié)點(diǎn)及其后代的XML(IE獨(dú)有)。
節(jié)點(diǎn)對(duì)象的方法
appendChild()—向節(jié)點(diǎn)的子節(jié)點(diǎn)列表的結(jié)尾添加新的子節(jié)點(diǎn)。
cloneNode()—復(fù)制節(jié)點(diǎn)。
hasChildNodes()—判斷當(dāng)前節(jié)點(diǎn)是否擁有子節(jié)點(diǎn)。
insertBefore()—在指定的子節(jié)點(diǎn)前插入新的子節(jié)點(diǎn)。
normalize()—合并相鄰的Text節(jié)點(diǎn)并刪除空的Text節(jié)點(diǎn)。
removeChild()—?jiǎng)h除(并返回)當(dāng)前節(jié)點(diǎn)的指定子節(jié)點(diǎn)。
replaceChild()—用新節(jié)點(diǎn)替換一個(gè)子節(jié)點(diǎn)。
IE6獨(dú)有
selectNodes()—用一個(gè)XPath表達(dá)式查詢選擇節(jié)點(diǎn)。
selectSingleNode()—查找和XPath查詢匹配的一個(gè)節(jié)點(diǎn)。
transformNode()—使用XSLT把一個(gè)節(jié)點(diǎn)轉(zhuǎn)換為一個(gè)字符串。
transformNodeToObject()—使用XSLT把一個(gè)節(jié)點(diǎn)轉(zhuǎn)換成為一個(gè)文檔。
NodeList對(duì)象
length –返回節(jié)點(diǎn)列表中的節(jié)點(diǎn)數(shù)目。
item()—返回節(jié)點(diǎn)列表中處于指定的索引號(hào)的節(jié)點(diǎn)。
例如:
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對(duì)象
length—返回列表中節(jié)點(diǎn)數(shù)目。
getNamedItem()—返回指定的節(jié)點(diǎn)。(通過(guò)名稱)
item()—返回處于指定索引號(hào)的節(jié)點(diǎn)。
removeNamedItem()—?jiǎng)h除指定的節(jié)點(diǎn)(根據(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”);
刪除第一個(gè)book元素的category屬性
Document對(duì)象代表整個(gè)XML文檔。
Document對(duì)象的屬性。
async—規(guī)定XML文件的下載是否應(yīng)當(dāng)被同步處理。
childNodes—返回屬于文檔的子節(jié)點(diǎn)的節(jié)點(diǎn)列表。
doctype—返回與文檔相關(guān)的文檔類型聲明。
documentElement—返回文檔的子節(jié)點(diǎn)。
firstChild—返回文檔的首個(gè)子節(jié)點(diǎn)。
implementation—返回處理該文檔的DOMImplementation對(duì)象。(IE沒有)
lastChild—返回文檔的最后一個(gè)子節(jié)點(diǎn)。
nodeType—返回節(jié)點(diǎn)類型。
nodeName—依據(jù)節(jié)點(diǎn)的類型返回其名稱。
nodeValue—依據(jù)節(jié)點(diǎn)的類型返回其值。
text—返回節(jié)點(diǎn)及其后代的文本(IE獨(dú)有)。
xml—返回節(jié)點(diǎn)及其后代的XML(IE獨(dú)有)。
Document對(duì)象的方法
createAttribute(att_name)—?jiǎng)?chuàng)建擁有指定名稱的屬性節(jié)點(diǎn),并返回新的屬性對(duì)象。
createCDATASection(data)—?jiǎng)?chuàng)建CDATA區(qū)段節(jié)點(diǎn)。
createComment(data)—?jiǎng)?chuàng)建注釋節(jié)點(diǎn)。
createDocumentFragment—?jiǎng)?chuàng)建空的DocumentFragment對(duì)象,并返回此對(duì)象。
createElement(node_name)—?jiǎng)?chuàng)建元素節(jié)點(diǎn)。
createEntityReference(name)—?jiǎng)?chuàng)建EntityReference對(duì)象,并返回此對(duì)象。(IE獨(dú)有)
createTextNode(data)—?jiǎng)?chuàng)建文本節(jié)點(diǎn)。
getElementById(elementid)—查找具有指定的唯一ID的元素。
getElementsByTagName(node_name)—返回所有具有指定名稱的元素節(jié)點(diǎn)。
例如:
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 對(duì)象的屬性
attributes—返回元素的屬性的NamedNodeMap
childNodes—返回元素的子節(jié)點(diǎn)的NodeList
firstChild—返回元素的首個(gè)子節(jié)點(diǎn)。
lastChild—返回元素的最后一個(gè)子節(jié)點(diǎn)。
nextSibling—返回元素之后緊跟的節(jié)點(diǎn)。
nodeName—返回節(jié)點(diǎn)的名稱。
nodeType—返回元素的類型。
ownerDocument—返回元素所屬的根元素(document對(duì)象)。
parentNode—返回元素的父節(jié)點(diǎn)。
previousSibling—返回元素之前緊跟的節(jié)點(diǎn)。
tagName—返回元素的名稱。
text—返回節(jié)點(diǎn)及其后代的文本。(IE-only)
xml—返回節(jié)點(diǎn)及其后代得XML。(IE-only)
Element對(duì)象的方法
appendChild(node)—向節(jié)點(diǎn)的子節(jié)點(diǎn)列表末尾添加新的子節(jié)點(diǎn)。
cloneNode(true)—克隆節(jié)點(diǎn)。
getAttribute(att_name)—返回屬性的值。
getAttributeNode(att_name)—以 Attribute 對(duì)象返回屬性節(jié)點(diǎn)。
getElementsByTagName(node_name)—找到具有指定標(biāo)簽名的子孫元素。
hasAttribute(att_name)—返回元素是否擁有指定的屬性。
hasAttributes()—返回元素是否擁有屬性。
hasChildNodes()—返回元素是否擁有子節(jié)點(diǎn)。
insertBefore(new_node,existing_node)—在已有的子節(jié)點(diǎn)之前插入一新的子節(jié)點(diǎn)。
removeAttribute(att_name)—?jiǎng)h除指定的屬性。
removeAttributeNode(att_node)—?jiǎng)h除指定的屬性節(jié)點(diǎn)。
removeChild(node)—?jiǎng)h除子節(jié)點(diǎn)。
replaceChild(new_node,old_node)—替換子節(jié)點(diǎn)。
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對(duì)象
Attr 對(duì)象表示 Element 對(duì)象的屬性。
name—返回屬性的名稱。
nodeName—返回節(jié)點(diǎn)的名稱,依據(jù)其類型
nodeType—返回節(jié)點(diǎn)的類型。
nodeValue—設(shè)置或返回節(jié)點(diǎn)的值,依據(jù)其類型
ownerDocument—返回屬性所屬的根元素(document對(duì)象)。
specified—如果屬性值被設(shè)置在文檔中,則返回 true,如果其默認(rèn)值被設(shè)置在 DTD/Schema 中,則返回 false。
value—設(shè)置或返回屬性的值。
text—返回屬性的文本。IE-only。
xml—返回屬性的 XML。IE-only。
Text對(duì)象的屬性
data—設(shè)置或返回元素或?qū)傩缘奈谋尽?/p>
length—返回元素或?qū)傩缘奈谋鹃L(zhǎng)度。
Text對(duì)象的方法
appendData(string)—向節(jié)點(diǎn)追加數(shù)據(jù)。
deleteData(start,length)—從節(jié)點(diǎn)刪除數(shù)據(jù)。
insertData(start,string)— 向節(jié)點(diǎn)中插入數(shù)據(jù)。
replaceData(start,length,string)—替換節(jié)點(diǎn)中的數(shù)據(jù)。
replaceData(offset)— 把一個(gè) Text 節(jié)點(diǎn)分割成兩個(gè)。
substringData(start,length)— 從節(jié)點(diǎn)提取數(shù)據(jù)。
關(guān)于JavaScript操作XML/HTML比較常用的對(duì)象屬性集錦的全部敘述就到此結(jié)束了,更多內(nèi)容請(qǐng)登陸腳本之家官網(wǎng)了解更多,謝謝。
相關(guān)文章
canvas壓縮圖片轉(zhuǎn)換成base64格式輸出文件流
本文主要介紹了canvas壓縮圖片轉(zhuǎn)換成base64格式輸出文件流的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
流量統(tǒng)計(jì)器如何鑒別C#:WebBrowser中偽造referer
這篇文章主要介紹了流量統(tǒng)計(jì)器如何鑒別C#:WebBrowser中偽造referer,需要的朋友可以參考下2015-01-01
JavaScript?12個(gè)有用的數(shù)組技巧
數(shù)組是Javascript最常見的概念之一,它為我們提供了處理數(shù)據(jù)的許多可能性,熟悉數(shù)組的一些常用操作是很有必要的。本文將為大家介紹12個(gè)有用的JavaScript數(shù)組技巧,需要的朋友可以參考一下2021-12-12
JS碰撞運(yùn)動(dòng)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JS碰撞運(yùn)動(dòng)實(shí)現(xiàn)方法,詳細(xì)分析了碰撞運(yùn)動(dòng)的原理及相應(yīng)的javascript實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12

