Javascript DOM, javascriptdom

Source: Internet
Author: User

Javascript DOM, javascriptdom

I. node levels

1. node Type

NodeName, nodeValue, and nodeType contain information about nodes.


Each node has a nodeType attribute.

Value-element type
1-ELEMENT
2-ATTRIBUTE
3-TEXT
4-CDATA
5-ENTITY REFERENCE
6-ENTITY
7-PI (processing instruction)
8-COMMENT
9-DOCUMENT
10-DOCUMENT TYPE
11-DOCUMENT FRAGMENT
12-NOTATION




The nodeName attribute contains the name of a node.

The nodeName of the element node is the label name.
The nodeName of the attribute node is the attribute name.
The nodeName of the text node is always # text
The nodeName of the document node is always # document

Note: The Tag Name of the XML element contained by nodeName is always capitalized.

NodeValue
For text nodes, the nodeValue attribute contains text.
For an attribute node, the nodeValue attribute contains the attribute value.
The nodeValue attribute is unavailable for document nodes and element nodes.


2. node relationship

Each node has a childNodes attribute, which can be accessed through square brackets or the item () method.

In addition, the following attributes are available:

ParentNode

Previussibling

NextSibling

FirstChild

LastChild


In addition, hasChildNodes () is used to determine whether a node contains subnodes.


3. Operation nodes

AppendChild () A PARAMETER

InsertBefore (): inserts the node represented by the first parameter before the node indicated by the second parameter. If the second parameter is null, the node is inserted to the end.

ReplaceChild (). Replace the node represented by the second parameter with the node represented by the first parameter.

RemoveChild () A PARAMETER

CloneNode () is a Boolean parameter. "true" indicates deep replication. It indicates that nodes and subnodes are copied. "false" indicates that only nodes do not copy subnodes.


Ii. Document Type

1. subnode of the document

Document.doc umentElement points to html

Document. body Points to body

Document.doc type

2. Document Information

Document. title

Document. URL

Document, domain

Document. referer


3. Search for elements

Document. getElementById ()

Document. getElementByTagName (): Return NodeList, which can be accessed through square brackets or item (). If the parameter is *, all elements in the document can be obtained.

NamedItem (): obtain from the element set by using the element name attribute, for example, document. getElementByTagName (). namedItem ()

Document. getElementsByName ()


4. Special set

Document. anchors: All <a> elements with the name feature in the document

Document. forms: All <form> elements in the document

Document. images: All elements in the document

Document. links: All <a> elements with the href feature in the document


5. DOM consistency Detection

Document. implementation. hasFeature ()


6. Document Writing

Document. write ()

Document. writeln ()

Document. open ()

Docuemnt. close ()


Iii. Element type

1. standard features of HTML elements

Id

Title

Lang

Dir

The value of className for class

These attributes can be read or assigned values.


2. Obtain features

GetAttribute ()


3. Set features and delete

SetAttribute ()


RemoveAttribute ()


4. attributes

Contains a NamedNodeMap. The method is as follows:

GetNamedItem (name)

RemoveNamedItem (name)

SetNamedItem (node)

Item (pos)


The corresponding methods include getAttribute (), setAttribute (), and removeAttribute ()


Iv. Text Type

Document. createTextNode ()

SplitText ()


V. Comment type


....



Dom Extension

1. Selector

QuerySelector () returns only the first matched element.

QuerySelectorAll () returns all matched elements


2. Element Traversal

Only elements are obtained, excluding comments and text nodes.

ChildElementCount Number of child elements

FirstElementChild

LastElementChild

PreviuosElementSibling

NextElementSibling


3. HTML5

GetElementByClassName ()


The classList attributes include:

Add ()

Contains ()

Remove ()

Toggle ()


Insert tag

InnerHTML attributes

OuterHTML attributes

InsertAdjacentHTML (): two parameters. The first parameter must be as follows:

Beforebegin

Afterbegin

Beforeend

Afterend


The scrollIntoView () parameter is true or false.


The children attribute, which is similar to childNodes, but children only includes child nodes of elements.


Insert text

InnerText

OuterText















Why should DOM be used in javascript?

First, you need to know what dom is.
Dom is not javascript, and dom is a document.
Dom is a set of web standards used to describe how scripts interact and access structured documents.
Dom defines a series of objects, methods, and attributes for accessing, operating, and creating content, structures, styles, and actions in a document.
Dom can be said to exist to obtain objects.
If you have other methods, you can replace dom.

How to create a dynamic layer with DOM in javascript,

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
</HEAD>

<BODY>
<Button onclick = "document. body. removeChild (document. getElementById ('dynamic '); this. style. display = 'none';"> Delete </button>
<Script language = "JavaScript">
<! --
// Var dydiv = document. createElement ("DIV ");
// Dydiv. setAttribute ("ID", "dynamic ");
// Dydiv.css Text = "position: absolute; left: 100px; top: 100px ";
// Var text = document. createTextNode ("you are welcome !!!! ");
// Dydiv. appendChild (text );
// Document. body. appendChild (dydiv );
// Change:
Document. write ("<div id = \" dynamic \ "style = \" position: absolute; left: 100px; top: 100px; \ "> You are welcome! </Div> ");
// -->
</SCRIPT>
</BODY>
</HTML>

Use your own:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = & quo ...... remaining full text>

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.