JavaScript DOM features and applications (1)

Source: Internet
Author: User

Because DOM is a tree structure, a Node is abstracted as an object Node, which is the core object of DOM. There are 12 types of nodes. The value of Node. nodeType is 1-12 ).

BKJIA recommended reading: The nature and operation methods of JavaScript DOM

The following are the types of nodes:

 
 
  1. Node. ELEMENT_NODE (1)
  2. Node. ATTRIBUTE_NODE (2)
  3. Node. TEXT_NODE (3) // <! [CDATA []> contains plain text. It does not have subnodes.
  4. Node. CDATA_SECTION_NODE (4) // The child Node must be TextNode.
  5. Node. ENTITY_REFERENCE_NODE (5)
  6. Node. ENTITY_NODE (6) // object definition in DTD <! ENTITY foo "foo">, no subnode
  7. Node. PROCESSING_INSTRUCTION_NODE (7) // PI, no subnode
  8. Node. COMMENT_NODE (8)
  9. Node. DOCUMENT_NODE (9) // Root element of the outermost layer, including all other nodes
  10. Node. DOCUMENT_TYPE_NODE (10) // DTD, <! DOCTYPE ......>
  11. Node. DOCUMENT_FRAGMENT_NODE (11)
  12. Node. NOTATION_NODE (12) // Nation definition in DTD
  13.  
  14. Node. ELEMENT_NODE (1)
  15. Node. ATTRIBUTE_NODE (2)
  16. Node. TEXT_NODE (3) // <! [CDATA []> contains plain text. It does not have subnodes.
  17. Node. CDATA_SECTION_NODE (4) // The child Node must be TextNode.
  18. Node. ENTITY_REFERENCE_NODE (5)
  19. Node. ENTITY_NODE (6) // object definition in DTD <! ENTITY foo "foo">, no subnode
  20. Node. PROCESSING_INSTRUCTION_NODE (7) // PI, no subnode
  21. Node. COMMENT_NODE (8)
  22. Node. DOCUMENT_NODE (9) // Root element of the outermost layer, including all other nodes
  23. Node. DOCUMENT_TYPE_NODE (10) // DTD, <! DOCTYPE ......>
  24. Node. DOCUMENT_FRAGMENT_NODE (11)
  25. Node. NOTATION_NODE (12) // Nation definition in DTD

Features/methods contained in the Node interface

◆ The nodeName attribute returns a string whose content is the name of the given node. If the node is an element node, the name of the element is returned. If the node is an attribute node, the name of the attribute is returned. If the node is a text node, a string with the content of # text is returned;

◆ The nodeType attribute returns an integer that represents the type of the given node;

◆ The nodeValue attribute returns the current value of the given node. if the node is an element node, null is returned. If the node is an attribute node, the name of the attribute is returned. If the node is a text node, the content of the text node is returned;

◆ OwnerDocument points to the document to which the node belongs;

◆ The attributes package Halle represents the Attr object of an Element. It is only used for Element nodes;

◆ List of all child nodes of childNodes;

◆ FirstChild points to the first node in the childNodes list;

◆ LastChild points to the last node in the childNodes list;

◆ NextSibling points to the next sibling node. If this node is the last sibling node, the value is null;

◆ Previussibling indicates a forward sibling node. If this node is the first sibling node, the value is null;

◆ ParentNode returns the parent node of a given node;

◆ HasChildNodes () returns true if childNodes contains one or more nodes;

◆ AppendChild (node) adds the node to the end of childNodes;

◆ RemoveChild (node) deletes the node from childNodes;

◆ InsertBefore (newnode refnode) inserts newnode before the refnode in childNodes.

 
 
  1. Var container = document. getElementById ("content ");
  2. Var message = document. getElementById ("fineprint ");
  3. Var para = document. createElement ("p ");
  4. Container. insertBefore (para, message );
  5.  
  6. Var container = document. getElementById ("content ");
  7. Var message = document. getElementById ("fineprint ");
  8. Var para = document. createElement ("p ");
  9. Container. insertBefore (para, message );
  10. ReplaceChild (newnode, oldnode) replaces oldnode in childNodes with newnode.
  11. Var container = document. getElementById ("content ");
  12. Var message = document. getElementById ("fineprint ");
  13. Var para = document. createElement ("p ");
  14. Container. replaceChild (para, message );
  15.  
  16. Var container = document. getElementById ("content ");
  17. Var message = document. getElementById ("fineprint ");
  18. Var para = document. createElement ("p ");
  19. Container. replaceChild (para, message); then get Node:
  20.  
  21. /* Use the document Object */
  22. Var oHtml = document.doc umentElement;
  23. /* Get
  24. Var oHead = oHtml. firstChild;
  25. Var oBody = oHtml. lastChild;
  26. /* This method can be used */
  27. Var oHead = oHtml. childNodes [0];
  28. Var oBody = oHtml. childNodes [1];
  29. /* You can also use the method to obtain the index value of the array */
  30. Var oHead = oHtml. childNodes. item (0 );
  31. Var oBody = oHtml. childNodes. item (1 );
  32. /* Use document. body to get <body/> */
  33. Var oBody = document. body;
  34.  
  35. /* Use the document Object */
  36. Var oHtml = document.doc umentElement;
  37. /* Get
  38. Var oHead = oHtml. firstChild;
  39. Var oBody = oHtml. lastChild;
  40. /* This method can be used */
  41. Var oHead = oHtml. childNodes [0];
  42. Var oBody = oHtml. childNodes [1];
  43. /* You can also use the method to obtain the index value of the array */
  44. Var oHead = oHtml. childNodes. item (0 );
  45. Var oBody = oHtml. childNodes. item (1 );
  46. /* Use document. body to get <body/> */
  47. Var oBody = document. body; using createElement (element)

Create a specified tag name and create a new element node. The returned value is the reference pointer pointing to the new element node.

 
 
  1. eg) var para = document.createElement("p");  
  2. document.body.appendChild(para); 


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.