This article: HTML DOM objects

Source: Internet
Author: User

HTML DOM Object

This article mainly introduces the HTML DOM object: Document, Element, Attr, event, and so on 4 objects.

Directory

1. Document object: Represents the root node of the document tree, and most of the properties and methods operate on the element.

2. Element object: Represents an element in a document that can be included as an element node, a text node, and an annotation node.

3. Attr Property Object: Represents a property of an element node. You can get, add, and modify the specified property.

4. Event object: Indicates the state at the time the event occurred, such as the element in which the event occurred, the state of the keyboard key, the position of the mouse, the state of the mouse button, and so on.

1. Document Object

The Document object is the root node of the documentation tree.

Gets a document object that is typically passed through the window's document property.

extended Read : https://developer.mozilla.org/en-US/docs/Web/API/document

1.1 Static properties

readonly [] document.all : Gets an array that contains all the elements within the HTML document.

readonly [] document.anchors : Gets an array that contains all the anchor elements within the HTML document.

ReadOnly body document.body : Gets the BODY element within the HTML document.

String document.charset : Sets or gets the character encoding of the document.

Cookie document.cookie : Sets or gets a cookie for the current HTML document.

String Document.designmode : Sets whether the document can be edited. On: document editable; off (default): Document is not editable.

ReadOnly string document.domain : Gets the domain name of the current HTML document.

readonly [] document.forms : Gets an array that contains all the form elements within the HTML document.

readonly [] document.images : Gets an array that contains all the image elements within the HTML document.

readonly [] document.links : Gets an array containing all the link elements within the HTML document

ReadOnly string document.referrer : Gets the URL of the source loaded into this document.

String document.title : Sets or gets the title of the HTML document.

ReadOnly string document. URL : Gets the URL of the HTML document.

1.2 Static methods

Attribute document.createattribute(attrname): Creates a Property object. Eg:document.createAttribute (' id '); Create an ID property

HtmlElement document.createelement(elementname): Creates an Element object. Eg:document.createElement (' a '); Create a element of a

HtmlElement document.createelementns(namespaceuri,attrname): Creates an Element object and specifies a namespace. Eg:document.createElementNS ("http://www.w3.org/1999/xhtml", "a");

EventType document.createevent(eventtype): Creates a specified event type.

Detailed Method

HtmlElement document.getElementById(elementid): Gets the first element of the specified ID.

Detailed Method

Htmlelement[] document.getelementsbyclassname(className): Gets an array of elements with a class property that contains the specified value.

Detailed Method

Htmlelement[] document.getelementsbyname(namevalue): Gets an array of elements of the Name property for the specified value.

Detailed Method

Htmlelement[] document.getelementsbytagname(elementname): Gets an array of elements that specify the element name.

Parameter: elementname {string}: The name of the element. such as: Div, A and so on. Return value: {htmlelement[]} returns an array of elements. Example: Var linkarray=document.getelementsbytagname (' a '); Get array of a elements

void document.write(String): writes content to an HTML document. Eg:document.write (' <div> Show div control </div> ');

void Document.writeln(String): writes content to an HTML document and wraps the line at the end.

2. Element Object

Description : The element object represents elements in a document and can be included as element nodes, text nodes, and annotation nodes.

Access : Available through document.getElementById (ElementID), Document.getelementsbyname (ClassName), Document.getelementsbyname (Namevalue), document.getElementsByTagName (elementname), and so on to get the specified element.

extended Read : https://developer.mozilla.org/en-US/docs/Web/API/Attr

2.1 Instance Properties

ReadOnly namednodemap attributes : Read-only, gets all the property collection objects for this element.

ReadOnly htmlelement[] children : Gets an array of the following elements of this element.

ReadOnly string[] classlist : Read-only, gets an array containing the class of the element. Eg:<button class= "Btn-show btn-submit" ></buttom> = [' btn-show ', ' btn-submit ']

String className : Sets or gets the class property of the element. Eg:<button class= "Btn-show btn-submit" ></buttom> = [' Btn-show btn-submit ']

String ID : Sets or gets the ID of the element.

String InnerHTML : Gets the contents of an element in HTML code format settings.

Detailed Method

String innerText : Sets or gets the text content within the element.

Detailed Method

Cssstyledeclaration style: Sets or gets the style of the element. Eg:element.style.width= ' 30px '; Set the width style of an element

ReadOnly string tagName : Gets the element name of the element, returned in uppercase format. eg:<div></div> = Element.tagname; Back to DIV

ReadOnly string NamespaceURI : Gets the namespace of the element. The default return is: "http://www.w3.org/1999/xhtml".

String title : Sets or gets the value of the element title property.

2.2 Example methods

void Blur(): The element loses focus. Eg:document.getElementById (' text '). blur ();

void Click(): The element simulates the click. Eg:document.getElementById (' text '). Click ();

void Focus(): The element gets the focus. Eg:document.getElementById (' text '). focus ();

String getattribute(attributename): Gets the value of the specified property, and returns null if the property does not exist. Eg:document.getElementById (' div '). getattribute (' name '); Gets the name property of the element

Boolean hasattribute(attributename): Determines whether the current element contains an attribute. Eg:document.getElementById (' div '). Hasattribute (' name '); = = true: Contains the name attribute

HtmlElement queryselector(cssselector): Gets the first matching child element that conforms to cssselector under the element. If not found, returns NULL.

Detailed Method

Htmlelement[] queryselectorall(cssselector): Gets an array that contains all the child elements under the element that match the cssselector. Cssselector specifications Refer to the previous method of the detailed.

void removeattribute(attributename): Deletes the specified attribute of the element. Returns undefined regardless of whether the element contains this attribute.

void SetAttribute(attributename,value): Sets the value of an attribute of the element. If this property does not exist in the element, the Add property operation is performed. Eg:document.getElementById (' div '). SetAttribute (' name ', ' Logdiv '); Set the Name property

3. Attr Property Object

description : The Attr object represents a property of an element node.

Get method : Element.attributes property gets the Attr object.

extended Read : https://developer.mozilla.org/en-US/docs/Web/API/Attr

3.1 Instance Properties

ReadOnly string name : Read-only, gets the property name. Eg:document.getElementById (' div '). attributes[0].name;//= ID: Gets the name of the first property

String value : Sets or gets the value of the property. Eg:document.getElementById (' div '). attributes[0].value;//= div: Gets the value of the first property

3.2 Methods

No

4. Event Object

Description : The event object represents the state at which the event occurred, such as the element in which the event occurred, the state of the keyboard key, the position of the mouse, the state of the mouse button, and so on.

Get method : Invokes the Window.event object when the first parameter is triggered by the event or when an event is triggered.

12 document.getElementById(‘btn‘).addEventListener(‘click‘, function(e){    console.log(window.event==e); // => true :触发时两者是一样

Bubbling event : When a child element fires an event, the parent element fires the same time (the event is allowed to bubble).

block subsequent handlers : through the AddEventListener () method, you can register multiple handlers for the same event of an element, and after you call the Stopimmediatepropagation () method in an event, Handlers that have been registered later will not be executed. For example, an element registers 3 functions on the Click event, calls the Stopimmediatepropagation () method on the 2nd function, and the 3rd function does not execute.

extended Read : https://developer.mozilla.org/en-US/docs/Web/API/Event

4.1 Instance Properties

ReadOnly Boolean Bubbles : Read-only, gets whether this event is bubbling.

ReadOnly boolean cancelable : Read-only to get whether this event can be revoked. True: The event can be revoked. You can call Preventdefault () to cancel the subsequent default action. Flase: The event is not revocable.

ReadOnly Object currenttarget : Read-only, gets the object that is handling this event, can be element, document object, and so on.

ReadOnly boolean defaultprevented : Read-only to indicate whether Preventdefault () has been called. True: The Preventdefault () method has been called.

readonly int eventphase : Read-only, which represents the processing phase of an event: 0 indicates that no processing is being processed, 1 represents the capture phase, 2 represents the target stage, and 3 represents the bubbling phase.

ReadOnly Object target : Read-only to get the objects that triggered this event.

ReadOnly long timeStamp : Read-only, gets the time when the event was created. Eg:console.log (E.timestamp); = 1455034313689

ReadOnly string type : Read-only, gets the event name (does not include the prefix on, such as: click)

ReadOnly boolean istrusted : Read-only to indicate whether this event is generated by the browser. True: generated by the browser. False: Created by JS, such as: Dispatchevent ().

4.2 Example methods

void Preventdefault(): The subsequent default action of the terminating event (the event is to be undone, that is, the Cancelable property is true). For example: CheckBox's Click event, execute this code, the element will not be ticked and unchecked.

Method Example

void stopimmediatepropagation(): A Subsequent handler that blocks the bubbling behavior of the current event and blocks the same type of event on this element.

Method Example

void stoppropagation(): Blocks the bubbling behavior of the current event.

Detailed Method

The difference between the 4.3 currenttarget and the target property

currenttarget : Gets the object that is handling this event.

target : Gets the object that triggered this event.

the difference between the two during the bubbling phase : Assume that both the body and the button element are registered with the Click event, and when the button element is clicked, the body's Click event is also triggered, and in this case, the click event of the body Currenttarget points to the BODY element, and target points to the button element.

Example : Display results when a button element is clicked

123456789 document.body.onclick = function(e) {    console.log(e.currentTarget); // 指向body元素    console.log(e.target); // 若是冒泡事件时,指向最开始触发的元素。否则为元素自身。};document.getElementById(‘btn‘).onclick = function(e) {    console.log(e.currentTarget);    console.log(e.target);};

  

================================== Series Article ==========================================

Reprint http://www.cnblogs.com/polk6/p/4957563.html

This article: HTML DOM objects

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.