JavaScript DOM Programming Art (2nd edition) reading notes (3)

Source: Internet
Author: User

Dom

DOM: Document Object model;

Node

Element node: The atom of the DOM is the element node. 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 an XHTML document, a text node is always contained inside 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 make 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 elements

getElementById, getElementsByTagName, getelementsbyclassname three ways to get an element node.

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

getElementById and getElementsByTagName can also be used together. As shown below:

var shopping = document.getElementById ("purchase");

var items = shopping.getelementsbytagname ("*");

This allows you to get the number of elements in an element that has an id attribute value of purchase.

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

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 start of the search in the DOM tree, and the second classname is the class name to search for.

Get and Set properties

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

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 the change through the browser's view source option to view the document's source code, that is, the changes made by setattribute are not reflected in the source code of the document itself. This "duplicity" phenomenon stems from the DOM's working mode: loading the static content of the document, and then dynamically refreshing, dynamic refresh does not affect the static content of the document. This is the true power of the DOM: refreshing the content of the page without having to refresh the page in the browser.

JavaScript DOM Programming Art (2nd edition) reading notes (3)

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.