Let's take a simple document tree first.
Obviously the top-level node of the tree is the Nodea node, followed by moving to any point in the tree by the specified suitable node, combining the following code you can better understand the relationship between this tree node: .1138611
Nodea.firstchild = nodea1
Nodea.lastchild = Nodea3
Nodea.childNodes.Length = 3
Nodea.childNodes [0] = nodea1
Nodea.childNodes [1] = nodea2
Nodea.childNodes [2] = nodea3
Nodea1.parentNode = Nodea
Nodea1.nextsibling = nodea2
Nodea3.prevsibling = nodea2
Nodea3.nextsibling = null
Nodea.lastchild.firstchild = Nodea3a
Nodea3b.ParentNode.ParentNode = Nodea
The DOM definition provides a practical method for operating a node structure of a document object, providing these common methods such as execution object insertion, update, deletion, cloning, etc. 1138611
INSERTBEFORE () - Insert a new child node before the reference child node. If the child node is null, the new child node will be inserted as the last child node of the call node. 1138611
Replacechild () - Instead of using the specified newchild in the ChildNodes collection specifier; if it is successful, return OldChild; if newchild is null, simply remove Oldchild .1138611
RemoveChild () - Remove the node specified by RemoveChild from the ChildNodes collection of nodes. If delete is successful, return the deleted child node .1138611
Appendchild () - Add a new node to the end of the ChildNodes collection, if successful, return to the new node .1138611
Clonenode () - Create a new, copy node, and if the incoming parameter is true, the child node will be copied. If the node is an element, then the corresponding attribute will be replicated, and the new node is returned. 1138611
In order to access or create a new node in a document tree, you can use the following methods: getElementByid () getElementsBytagname () CreateElement () createttribute () CreateElement () CreateTextNode () Note: Only one document object in a page, except getElementsBytagname Outside, other methods can only be called via Document.MethodName (). 1138611
Look at the following example: .1138611
hEAD>
this is a sample paragraph. p>
Alert (Document.Documentelement.lastchild.FirstChild.tagname);
// ->
Script>
body>
html>
The results will show "P", here are some explanations document.documentElement - gives the page's HTML tag lastChild -. Gives the BODY tag firstChild -. Gives the first element in the BODY tagName -. Gives that element's tag name, "P" In this case..1138611 another: .1138611
hEAD>
this is a sample paragraph. p>
Alert (Document.Documentelement.lastchild.FirstChild.tagname);
// ->
Script>
body>
html>
This example is not a big difference, but only one blank line, but in NS, it will automatically add a node for the blank line, so the return value is "undefined", and the jump in IE Still pointed to the P tag. 1138611
More commonly used methods: .1138611
this is a sample paragraph. p>
...
Alert (Document.GetElementByid ("MyParagraph"). Tagname);
This approach You don't have to care about which place in the document tree, and just guarantees that its ID number is the only one. 1138611
Next, an access element node is document.getlementsBytagName (), which is an array, for example, you can change the connection of the entire page by the following example. 1138611
Var nodelist = document.getlementsBytagname ("a");
For (var i = 0; i Nodelist [i] .style.color = "# ff0000"; Attribute and Attributes Attribute objects and elements are associated, but it is not considered part of the document tree, so the attribute cannot be used as part of the child node collection. There are three ways to establish new properties for the elements. 1138611 1. Var attr = document.createattribute ("Myattribute"); Attr.Value = "MyValue"; Var el = document.getlementByid ("MyParagraph"); El.setttributenode (Attr); 2. Var el = document.getlementByid ("MyParagraph"); El.SetaTRibute ("Myattribute", "MyValue"); 3. Var el = document.getlementByid ("MyParagraph"); El.myattribute = "myvalue"; You can define your own properties in the HTML tag: .1138611 this is a sample paragraph. p> ... Alert (Document.GetElementByid ("MyParagraph"). GetAttribute ("MyAttribute"); The return value will be "MyValue". But please note that you must use GetAttribute instead of AttributeName, because some browsers do not support custom properties .1138611 Attribute can also be easily removed from one element, you can use removeAttribute () or pointing Element.attributeName to a null value... We can produce some dynamic effects through Attributes: .1138611 text in a paragraph element. p> ... Code for the Links ... Document.getElementByid ('Sample1'). SetAttribute ('align', 'left'); Document.getlementByid ('Sample1'). SetAttribute ('align', 'right'); Alternatively: .1138611 Text in a paragraph Element. p> ... Code for the Links ... Document.getLementByid ('Sample2'). style.textalign = 'left'; Document.getLementByid ('Sample2'). style.textalign = 'right'; Like the above example, the properties available to modify the style can be modified by elements, even in the class. The only thing to mention is that TextAlign is evolved from the text-align in the style, there is a basic law, that is The attribute in Style If any -, in the DOM will be removed and the subsequent letters will be changed to uppercase, and some are if there is no style attribute even if the element is not used. 1138611 Text Nodes: Let's take a look at the example .1138611 this is the initial text. p> ... Code for the Links ... Document.getElementByid ('Sample1'). Firstchild.nodeValue = 'Once Upon a Time ...'; Document.getElementByid ('Sample1'). Firstchild.nodeValue = '... in a Galaxy Far, Far Away'; First of all, Text Nodes has no ID attribute as Elements, all it does not directly pass document.getlementById () or document.getlementsBytagname (), you will know more, you can see some can see Document.GtelementByid ('Sample1 ') .firstchild.nodeValue can read or set the value of Text Nodes. 1138611 Another more complicated example .1138611 this is the initial b> text. p> Its document structure is here via Document.GtelementByid ('Sample1'). Firstchild.NodeValue tells me only "this IS" "and INITIAL text will not change. Everyone should see it and the innerhtml. Of course you can use it. 1138611 Document.getElementByid ('Sample3'). Firstchild.nodeValue = ' ONCE B> UPON A TIME ...'; Document.getElementByid ('Sample3'). Firstchild.nodeValue = '... in a galaxy far, far i> away'; The HTML code will not be interpreted, and the browser will show them as ordinary text. 1138611 Create and delete text nodes: Var mytextNode = document.createtextNode ("My Text"); With the above code you can create a new text node, but it is not part of the document tree. To make it displayed on the page, you must make it a child of a node in the document tree, because Text Nodes can't have a son. So you can't join it into a text nodes, Attribute is not part of the document tree, this way is not, now only Elements Nodes, the following example shows how to add and remove a text node.1138611 Initial text within a paragraph element. p> ... Code to Add A Text Node ... Var text = document.createtextNode ("New Text" ( Counter1)); VAR EL = Document.GtelementByid ("Sample1"); El.Appendchild (Text); ... Code to Remove The Last Child Node ... VAR EL = Document.GtelementByid ("Sample1"); IF (el.haschildnodes ()) El.RemoveChild (el.lastchild); Increasing the text is very easy, the above code establishes a new Text Node and adds it to the end of the childnodes array through the appendchild () method, and sets a global variable of Counter1 to observe the return value of HaschildNodes (). True or false; used to determine if there is a child to prevent errors generated when it is called when there is no Child. 1138611 Create ELEMENT NODES With the above foundation, it should be more likely to understand, first look at the following code. 1138611 ... Code for the link ... Var Parael, Boldel; Parael = Document.createElement ("P"); Boldel = Document.createElement ("b"); Parael.Appendchild (Document.createTextNode ("this is a new paragraph with"); boldel.appendchild (document.createtextNode Parael.Appendchild (Boldel); Parael.Appendchild (Document.createTextNode ("Text Added to the Div"); Document.GetElementByid ("Sample1"). Appendchild (Paael); You can also set Attribute directly for new Element Nodes, you can: .1138611 boldel.style.color = "# fff00"; Parael.Appendchild (Boldel); Or: .1138611 Parael.Appendchild (Boldel); boldel.style.color = "# fff00";