JavaScript DOM Programming Art The DOM Foundation _javascript skills of Reading notes

Source: Internet
Author: User

Dom

DOM: Document Object model;

Node

Element node: The atoms of the DOM are element nodes. Elements such as <body>, <p>, <ul>. An element can contain other elements. The only element that is not contained in other elements is the

Text nodes: In XHTML documents, text nodes are always contained within an element node.

Attribute node: The attribute node is used to make a more specific description of the element. For example, almost every element has a title attribute, and we can use this property to give an accurate description of what is contained in the element:

<p title= "A gentle reminder" >don ' t forget to buy this stuff.</p>

In the DOM, title= "A gentle reminder" is an attribute node.

Css

get Element
getElementById, getElementsByTagName, getelementsbyclassname three ways to get element nodes.

getElementsByTagName allows a wildcard to be used as its argument, which means that every element in the document occupies a place in the array returned by the function. The wildcard character ("*") must be in quotation marks, which is intended to be different from the multiplication operation.

Also can combine getElementById and getelementsbytagname to use. As shown below:

Copy Code code as follows:

var shopping = document.getElementById ("purchase");
var items = shopping.getelementsbytagname ("*");

This allows you to get the number of elements in the element package with the id attribute value of purchase.

The Getelementsbyclassname method is supported only by newer browsers. To make up for this, Dom scripting programmers need to use the existing Dom method to implement their own getelementsbyclassname. In most cases, their implementation is roughly similar to the following getelementsbyclassname:

Copy Code code as follows:

function getelementsbyclassname (node, classname) {
if (node.getelementsbyclassname) {
return Node.getelementsbyclassname (classname);
}else{
var results = new Array ();
var elems = node.getelementsbytagname ("*");
for (Var i=0;i<elems.length;i++) {
if (Elems[i].classname.indexof (className)!=-1) {
Results[results.length] = elems[i];
}
}
return results;
}
}

The Getelementsbyclassname function accepts two parameters, the first node represents the search starting point in the DOM tree, and the second classname is the name of the class to search for.

Getting and setting properties

GetAttribute is a function that has only one argument-the name of the attribute you intend to query:

Copy Code code as follows:

Object.getattribute (attribute)

SetAttribute () allows us to make changes to the value of the attribute node. After making changes to the document through SetAttribute, you will still see the value of the property before you change it through the browser's view source option, which means that the changes made by setattribute will not be reflected in the source code of the document itself. This "duplicity" phenomenon derives from the working mode of the DOM: loading the file's static content first, then dynamically refreshing, and dynamic refreshing does not affect the static content of the document. This is the true power of DOM: refreshing the content of the page without having to refresh the page in the browser.

Related Article

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.