search
HomeWeb Front-endJS TutorialA collection of commonly used object attributes for JavaScript operations on XML/HTML_javascript skills

Node object properties

childNodes—returns the node list from node to child node

firstChild—Returns the first child node of the node.
lastChild—Returns the last child node of the node.

nextSibling—Returns the sibling node immediately following the node.

nodeName—Returns the name of the node, according to its type.
nodeType—Returns the type of node.
nodeValue—Sets or returns the value of a node, according to its type.

ownerDocument—Returns the root element of the node (document object).

parentNode—Returns the parent node of the node.

previousSibling—Returns the sibling node immediately preceding the node.

text—Returns the text of the node and its descendants (IE only).

xml - Returns the XML of the node and its descendants (IE only).

Methods of node objects

appendChild()—Adds a new child node to the end of the node's child node list.

cloneNode()—Copy node.

hasChildNodes()—determines whether the current node has child nodes.

insertBefore()—Inserts a new child node before the specified child node.

normalize()—Merge adjacent Text nodes and delete empty Text nodes.

removeChild()—Removes (and returns) the specified child node of the current node.

replaceChild()—Replace a child node with a new node.

Exclusively for IE6

selectNodes()—Query select nodes using an XPath expression.

selectSingleNode()—Finds a node that matches the XPath query.

transformNode()—Convert a node to a string using XSLT.

transformNodeToObject()—use XSLT to transform a node into a document.

NodeList object

length – Returns the number of nodes in the node list.

item()—Returns the node at the specified index number in the node list.

For example:

Javascript code

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

Output: title element:4

Javascript code

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

Output: book

NamedNodeMap object

length—Returns the number of nodes in the list.

getNamedItem()—Returns the specified node. (by name)

item()—Returns the node at the specified index number.

removeNamedItem()—Removes the specified node (based on name).

For example:

Javascript code

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

Output: 1

Javascript code

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

Output: COOKING

Javascript code

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

Delete the category attribute of the first book element

Document object represents the entire XML document.

Properties of Document object.

async—Specifies whether the download of XML files should be processed synchronously.

childNodes—Returns a list of nodes that are child nodes of the document.

doctype—Returns the document type declaration associated with the document.

documentElement—Returns the child node of the document.

firstChild—Returns the first child node of the document.

implementation—Returns the DOMImplementation object that handles this document. (IE does not have it)

lastChild—Returns the last child node of the document.

nodeType—Returns the node type.

nodeName—Returns the name of the node based on its type.

nodeValue—Returns the value of the node based on its type.

text—Returns the text of the node and its descendants (IE only).

xml - Returns the XML of the node and its descendants (IE only).

Methods of Document object

createAttribute(att_name)—Creates an attribute node with the specified name and returns a new attribute object.
createCDATASection(data)—Creates a CDATA section node.
createComment(data)—Creates a comment node.
createDocumentFragment—Creates an empty DocumentFragment object and returns this object.
createElement(node_name)—Creates an element node.
createEntityReference(name)—Creates an EntityReference object and returns this object. (IE only)
createTextNode(data)—Creates a text node.
getElementById(elementid)—Finds an element with a specified unique ID.
getElementsByTagName(node_name)—Returns all element nodes with the specified name.

For example:

Javascript code

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比较常用的对象属性集锦的全部叙述就到此结束了,更多内容请登陆脚本之家官网了解更多,谢谢。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 jQuery Fun and Games Plugins10 jQuery Fun and Games PluginsMar 08, 2025 am 12:42 AM

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

How do I create and publish my own JavaScript libraries?How do I create and publish my own JavaScript libraries?Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

jQuery Parallax Tutorial - Animated Header BackgroundjQuery Parallax Tutorial - Animated Header BackgroundMar 08, 2025 am 12:39 AM

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

How do I optimize JavaScript code for performance in the browser?How do I optimize JavaScript code for performance in the browser?Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Getting Started With Matter.js: IntroductionGetting Started With Matter.js: IntroductionMar 08, 2025 am 12:53 AM

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

Auto Refresh Div Content Using jQuery and AJAXAuto Refresh Div Content Using jQuery and AJAXMar 08, 2025 am 12:58 AM

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools