JavaScript node operations and DOMDocument properties and methods _javascript tips

Source: Internet
Author: User
Tags cdata html tags object model readable tag name

Property:
1Attributes Storage node's list of properties (read-only)
2childNodes Storage node List of child nodes (read only)
3dataType returns the data type of this node
4Definition definition of a node given in a DTD or XML schema (read-only)
5Doctype Specify document Type node (read-only)
6documentElement returns the root element of the document (Readable and writable)
7firstChild returns the first child node of the current node (read-only)
8Implementation return Xmldomimplementation Object
9lastChild returns the last child node of the current node (read-only)
10nextSibling returns the next sibling node of the current node (read-only)
11nodeName returns the name of the node (read only)
12nodeType return type of node (read only)
13nodeTypedValue Storage node value (readable and writable)
14nodeValue returns the node's text (Readable and writable)
15ownerDocument returns the root document containing this node (read-only)
16parentNode Return parent node (read only)
17Parsed returns whether this node and its child nodes have been resolved (read only)
18Prefix return namespace prefix (read only)
19PRESERVEWHITESPACE Specifies whether to leave blank (readable and writable)
20previousSibling returns the previous sibling node of this node (read only)
21Text returns the text content of this node and its descendants (readable and writable)
22url returns the URL of the most recently loaded XML document (read-only)
23XML XML representation of a node and its descendants (read-only)
Method:
1appendChild adds a new child node to the current node, after the last child node
2cloneNode returns the copy of the current node
3createAttribute Create a new property
4createCDATASection Create a CDATA segment that includes the given data
5createComment Create a comment node
6createDocumentFragment Create DocumentFragment Object
7createElement Create an element node
8createEntityReference Create EntityReference Object
9createNode Create a node of a given type, name, and namespace
10createPorcessingInstruction Create operation instruction node
11createTextNode Create a text node that includes the given data
12getElementsByTagName returns the collection of elements of the specified name
13hasChildNodes returns whether the current node has child nodes
14insertBefore inserting child nodes before the specified node
15Load import XML document at specified location
16loadXML XML document that imports the specified string
17removeChild deletes the specified child node from the list of child nodes
18replaceChild replaces the specified child node from the list of child nodes
19Save Save the XML file to the specified node
20selectNodes a specified match to the node and returns a list of matching nodes
21selectSingleNode a specified match to the node and returns the first matching node
22transformNode converts a node and its descendants using the specified style sheet
23transformNodeToObject converts a node and its descendants to an object using the specified style sheet
*********************************
DOM (Document Object model)
The introduction of the DOM (Document Object model) concept, which makes HTML even more powerful, but some of the DHTML friends are still somewhat scratched, just because the current manual is not very scientific writing, is by letter
To divide, inconvenient to consult. In fact, the most important thing in DOM is to master the relationship between nodes and nodes (between node Andnode), to learn the DOM in DHTML. Don't look through the
Some properties and methods, you have three Kingdoms when ZS "photographic memory" ability? No, then listen to me to analyze:
What Dom teaches us is a hierarchy that you can understand as a tree structure, like our directory, a root directory, a subdirectory under the root directory, and a child under a subdirectory.
Directory......
Root node:
Dom refers to each object in the hierarchy as a node, an example of an HTML Hypertext Markup Language: One root of the entire document is <HTML&GT, which can be used in the DOM
Document.documentelement to access it, which is the root node of the entire node tree (root)
Child nodes:
In the general sense of the node, the root node below the largest child node is the main document area <body>, to access the body tag, in the script should write:
Document.body
All text and HTML tags within the body area are the nodes of the document, called text nodes, element nodes (or tag nodes), and you know that HTML is just text,
No matter what kind of Web page must be composed of these two nodes, it can only be composed of these two nodes
The relationship between nodes:
The relationship between the nodes is also one of the most important joints in the DOM, how to correctly refer to the node object, it is necessary to understand the node tree each node of each other description way, in DHTML,
JavaScript scripts use a set of methods and attributes for each node object to describe another node object.
An absolute reference to a node:
Returns the root node of a document
Document.documentelement
Returns the tab node that is being clicked in the current document
Document.activeelement
Returns the source node from which the mouse is moved
Event.fromelement
Returns the source node where the mouse is moved
Event.toelement
Returns the source node of the activation event
Event.srcelement
The relative reference of the node: (set node for current nodes)
Return parent Node
Node.parentnode
Node.parentelement
Returns a collection of child nodes (including text nodes and label nodes)
Node.childnodes
Return child label Node collection
Node.children
Returns a collection of child text nodes
Node.textnodes
Returns the first child node
Node.firstchild
Returns the last child node
Node.lastchild
Returns the same genus next node
Node.nextsibling
Returns the same last node
Node.previoussibling
The various operations of the node: (Set the current node to nodes)
New label node handle:
Document.createelement (Snode)//parameter is the node tag name to be added, example: Newnode=document.createelement ("div");
1, add the node:
Append child nodes:
Node.appendchild (onode)//onode new node handle for a living, example: Node.appendchild (NewNode)
Apply Label node
Node.applyelment (Onode,swhere)//onode A new node handle for a living, Swhere has two values: Outside/inside, added to the current node or inside
Insert Node
Inode.insertbefore ()
Node.insertadjacentelement ()
Node.replaceadjacenttext ()
2, modify the node:
Delete a node
Node.remove ()
Node.removechild ()
Node.removenode ()
Replacing nodes
Node.replacechild ()
Node.replacenode ()
Node.swapnode ()
2, Replication node:
Returns a replication Replication node reference
Node.clonenode (BALL)//ball is a Boolean value true/false whether to clone all child nodes of the node
3, node information
Whether to include a node
Node.contains ()
Whether there are child nodes
Node.haschildnodes ()
*******************************************************
The following is JavaScript manipulating XML

Copy Code code as follows:

<script language= "JavaScript" >
<!--
var doc = new ActiveXObject ("Msxml2.domdocument"); Ie5.5+,createobject ("Microsoft.XMLDOM")
Loading documents
Doc.load ("B.xml");
To create a file header
var p = doc.createprocessinginstruction ("xml", "version= ' 1.0 ' encoding= ' gb2312 '");
Add File Headers
Doc.appendchild (P);
To get the root contact when loading directly
var root = doc.documentelement;
Create root contacts in two ways
var root = doc.createelement ("Students");
var root = Doc.createnode (1, "Students", "");
Creating Child contacts
var n = doc.createnode (1, "Ttyp", "");
Specify Child Contact text
N.text = "This is a test";
Create Sun Contact
var o = doc.createelement ("Sex");
O.text = "male"; Specify its text
Creating properties
var r = doc.createattribute ("id");
r.value= "Test";
Add properties
N.setattributenode (R);
Create a second property
var r1 = Doc.createattribute ("class");
R1.value= "TT";
Add properties
N.setattributenode (R1);
Delete Second Property
N.removeattribute ("class");
Add Sun Contact
N.appendchild (o);
Add text Contacts
N.appendchild (Doc.createtextnode ("This is a text node.");
Add a Comment
N.appendchild (Doc.createcomment ("This is a comment\n"));
Add Child contacts
Root.appendchild (n);
Copy Contacts
var m = N.clonenode (true);
Root.appendchild (m);
Delete a contact
Root.removechild (root.childnodes (0));
Create a data segment
var c = doc.createcdatasection ("This is a CDATA");
C.text = "Hi,cdata";
Adding data segments
Root.appendchild (c);
Add Root Contact
Doc.appendchild (root);
Find Contacts
var a = Doc.getelementsbytagname ("Ttyp");
var a = Doc.selectnodes ("//ttyp");
Show the properties of the modified contact
for (var i= 0;i<a.length;i++)
{
alert (a[i].xml);
for (Var j=0;j<a[i].attributes.length;j++)
{
alert (a[i].attributes[j].name);
}
}
modifying nodes, using XPath to locate nodes
var B = Doc.selectsinglenode ("//ttyp/sex");
B.text = "female";
alert (doc.xml);
XML save (required on server, client with FSO)
Doc.save ();
View the root contact XML
if (n)
{
alert (n.ownerdocument.xml);
}
-->
</script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.