How to operate domdocument attributes and methods on JavaScript nodes

Source: Internet
Author: User

 Recently, it was found that the domdocument object is very important, and XMLHTTP is also very important <br/> note that case sensitivity must not be mistaken. <br/> attributes: <br/> 1 attribute list of the attributes storage node (read-only) <br/> 2. List of child nodes of the childnodes storage node (read-only) <br/> 3 datatype: return the Data Type of this node <br/> 4. Definition of the node defined in DTD or XML mode (read-only) <br/> 5. doctype: Specifies the file type node (read-only) <br/> 6. documentelement: returns the root element of the document (read/write) <br/> 7. firstchild: return the first subnode of the current node (read-only) <br/> 8 Implementation: returned xmldomimplementation object <br/> 9 lastchild: returned last child node of the current node (read-only) <br/> 10 nextsibling: returned current Node's next sibling node (read-only) <br/> 11 nodename return node name (read-only) <br/> 12 nodetype return node type (read-only) <br/> 13 nodetypedvalue storage node value (read/write) <br/> 14 nodevalue return node text (read/write) <br/> 15 ownerdocument: return the root document containing this node (read-only) <br/> 16 parentnode: return the parent node (read-only) <br/> 17. parsed: indicates whether the node and Its subnodes have been parsed (read-only). <br/> 18. Prefix: returns the namespace prefix (read-only) <br/> 19 preservewhitespace: whether to retain white space (read/write) <br/> 20 previoussibling: return the previous sibling node of this node (read-only) <br/> 21 text: return the text content of the node and its descendants (read/write) <br/> 22url: return the U of the recently loaded XML file RL (read-only) <br/> 23xml returns the XML Representation of the node and its descendants (read-only) <br/> method: <br/> 1 appendchild adds a new subnode to the current node, <br/> 2 clonenode returns a copy of the current node <br/> 3 createattribute creates a new attribute <br/> 4 createcdatasection creates a CDATA segment containing the given data <br/> 5 createcomment create a comment node <br/> 6. createdocumentfragment create a documentfragment object <br/> 7. createelement create an element node <br/> 8. createentityreference create an entityreference object <br /> 9 create a given type for createnode, name and namespace node <br/> 10 createpo Rcessinginstruction create operation command node <br/> 11 createtextnode create a text node containing the given data <br/> 12 getelementsbytagname returns the element set with the specified name <br/> 13 haschildnodes returns whether the current node is there are sub-nodes <br/> 14 insertbefore insert sub-nodes before the specified node <br/> 15 load import the XML document at the specified position <br/> 16 loadxml import the XML document of the specified string <br/> 17 removechild deletes the specified child node from the child node list <br/> 18 replaceChild replaces the specified child node from the child node list <br/> 19 save Save the XML file to the specified node <br/> 20 selectnodes: matches the specified node, returns the matched node list. <br/> 21. selectsinglenode matches the specified node and returns A matching node <br/> 22 transformnode converts nodes and their descendants using the specified style sheet <br/> 23 transformnodetoobject converts nodes and their descendants to objects using the specified style sheet </ p> <p> ******************************* </ p> <p> dom (Document Object Model) <br/> the introduction of the DOM (Document Object Model) concept makes HTML even more powerful, but some friends who have learned DHTML are still a bit difficult, the reason is that the current manual is not scientifically written and is divided by letter. in fact, the most important thing in Dom is to master the relationship between nodes (between node andnode). To learn the DOM in DHTML, never read all the attributes and methods of the DHTML. <br/>, do you have the ability of Zhang song to "Never forget" when you are in Three Kingdoms? No, let me analyze it: <br/> In fact, Dom teaches us a hierarchy. You can think of it as a tree structure, just like our directory, A root directory with sub-directories and sub-directories <br/> ...... <Br/> root node: </P> <p> Dom calls every object in the hierarchy a node. Take HTML hypertext markup language as an example: the root of the entire document is <HTML>. In Dom, you can use <br/> document.doc umentelement to access it. It is the root node of the entire node tree) <br/> subnode: <br/> generally, the maximum subnode below the root node is the main document area <body>. Access the body label, in the script, write: <br/> document. body <br/> all text and HTML tags in the body area are document nodes, which are called text nodes and element nodes (or tag nodes ), as we all know, HTML is just text. <br/> no matter what the webpage is, it must be composed of these two nodes and can only be composed of these two nodes <br/> the relationship between nodes: <br/> the relationship between nodes is also one of the most important joints in Dom. To correctly reference a Node object, you must be clear about each node in the node tree. In DHTML, <br/> the Javascript script uses a complete set of methods and attributes of each node object to describe other node objects. </P> <p> absolute reference of a node: <br/> return to the root node of the document <br/> document.doc umentelement <br/> return to the tag node that is being clicked in the current document <br/> document. activeelement <br/> return the source node removed from the mouse <br/> event. fromelement <br/> return the source node to which the cursor is moved <br/> event. toelement <br/> return the source node of the activation event <br/> event. srcelement <br/> relative reference of a node: (set the current peer node to node) <br/> return to the parent node <br/> node. parentnode <br/> node. parentelement <br/> return the child node set (including text nodes and label nodes) <br/> node. childnodes <br/> Returns a set of child tag nodes <br/> node. children <br/> return the child text node set. <Br/> node. textnodes <br/> returns the first subnode <br/> node. firstchild <br/> returns the last subnode <br/> node. lastchild <br/> returns the same node as the next node <br/> node. nextsibling <br/> returns the same node as the previous one <br/> node. previussibling <br/> node operations: (set the current node to node) <br/> Add a label node handle: <br/> document. createelement (snode) // The name of the node label to be added, for example, newnode = document. createelement ("Div"); <br/> 1. Add a node: <br/> append a subnode: <br/> node. appendchild (onode) // node handle that is newly added for a living, for example, node. appendchild (newnode) <Br/> application label node <br/> node. applyelment (onode, swhere) // node handle newly added by onode. swhere has two values: Outside/inside, add it outside the current node or <br/> insert a node <br/> inode. insertbefore () <br/> node. insertadjacentelement () <br/> node. replaceadjacenttext () <br/> 2. modify a node: <br/> delete a node <br/> node. remove () <br/> node. removechild () <br/> node. removenode () <br/> replace node <br/> node. replaceChild () <br/> node. replacenode () <br/> node. swapnode () </P> <p> 2. Copy node: <br/> copy is returned. Copy node reference <br/> node. clonenode (ball) // ball is a Boolean value, true/false: whether to clone all child nodes of the node <br/> 3. Whether the node Information <br/> contains a node <br/> node. contains () <br/> whether a subnode exists <br/> node. haschildnodes () <br/> ************************************ * ***************** <br/> the following section describes the Javascript operation XML <br/> <MCE: script Language = "JavaScript"> <! -- <Br/> var Doc = new activexobject ("msxml2.domdocument"); // ie5.5 +, Createobject ("Microsoft. xmldom ") </P> <p> // load the document <br/> // Doc. load ("B. XML "); <br/> // create a file header <br/> var P = Doc. createprocessinginstruction ("XML", "version = '1. 0 'encoding= 'gb2312' "); <br/> // Add a file header <br/> Doc. appendchild (p); <br/> // get the root contact when loading directly <br/> // var root = doc.doc umentelement; <br/> // create a root contact in two ways <br/> // var root = Doc. createelement ("Students"); <br/> var root = Doc. createnode (1, "Students", ""); <br/> // create a subpoint <br/> var n = Doc. createnode (1, "ttyp", ""); <br/> // specifies the stator contact text <br/> // n. TEXT = "this is a test"; </P> <p> // create a sun contact <br/> var o = Doc. createelement ("sex"); <br/> O. TEXT = "male"; // specify the text <br/> // create an attribute <br/> var r = Doc. createattribute ("ID"); <br/> r. value = "test"; <br/> // Add attribute <br/> N. setattributenode (r); <br/> // create the second attribute <br/> var R1 = Doc. createattribute ("class"); <br/> r1.value = "TT"; </P> <p> // Add attribute <br/> N. setattributenode (R1); <br/> // Delete the second attribute <br/> N. removeattribute ("class"); <br/> // Add a sun contact <br/> N. appendchild (o); <br/> // Add text contacts <br/> N. appendchild (Doc. createtextnode ("this is a text node. "); <br/> // Add a comment <br/> N. appendchild (Doc. createcomment ("this is a comment/N"); </P> <p> // Add sub-Contacts <br/> root. appendchild (n); </P> <p> // copy contacts <br/> var M = n. clonenode (true); <br/> root. appendchild (m); </P> <p> // delete a contact <br/> root. removechild (root. childnodes (0); <br/> // create a data segment <br/> var c = Doc. createcdatasection ("this is a CDATA"); <br/> C. TEXT = "Hi, CDATA"; <br/> // Add Data Segment <br/> root. appendchild (c); </P> <p> // Add a root contact <br/> Doc. appendchild (Root); <br/> // search for contacts <br/> var A = Doc. getelementsbytagname ("ttyp"); <br/> // var A = Doc. selectnodes ("// ttyp"); <br/> // display the attributes of the change contact <br/> for (VAR I = 0; I <. length; I ++) <br/>{< br/> alert (A [I]. XML); <br/> for (var j = 0; j <A [I]. attributes. length; j ++) <br/>{< br/> alert (A [I]. attributes [J]. name); <br/>}< br/> // modify the node and use XPath to locate the node <br/> var B = Doc. selectsinglenode ("// ttyp/sex"); <br/> B. TEXT = ""; <br/> // alert (Doc. XML); <br/> // Save the XML file (FSO must be used on the server and the client) <br/> // Doc. save (); </P> <p> // view the root contact XML <br/> If (n) <br/> {<br/> alert (N. ownerdocument. XML); <br/>}< br/> // --> </MCE: SCRIPT> </P> <p>-

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.