JavaScript DOM, javascriptdom
JavaScript DOM
I. Introduction
The relationship between JavaScript and DOM has been mentioned earlier:
DOM (document object model) is a set of methods to abstract and conceptual the document content, and provides APIs for manipulating browser content.
All operations performed by JavaScript on webpages are performed by DOM to change the content and method of browser rendering. DOM is the core content defined in the browser rather than the JavaScript language specification.
This article focuses on DOM.
Ii. DOM
D -- Document: DOM is automatically generated when a webpage is created and loaded into a web browser. Converts a webpage document into a document object.
O -- Object: Object. For example, the document object that converts a webpage document. Objects in JavaScript are divided into three types (as described in the previous article): user-defined object, nativeobject, and host object ).
M -- Model or Map: the representation of something.
Iii. Nodes
Elementnode: it is the DOM atom, corresponding to elements in the document, such as div, p, li, and other element nodes.
Textnode: it is always contained in element nodes.
Attributenode: used to describe elements in more detail.
Iv. CSS
Cascading StyleSheet: changes the document presentation form, has the inheritance feature, that is, the style added to an element also applies to all the elements contained in this element, but can be overwritten.
5. Common DOM Methods
1. Get element -- used for document object!
/** * Get element */// reture one elementvar element = document.getElementById('id');//reture an array.var elements = document.getElementsByTagName('tag');//reture an array.var elements = document.getElementsByClassName('class');
If getElementsByClassName does not work, you can implement it as follows:
function getElementsByClassName (node, className) {if (ndoe.getElementsByClassName) {return node.getElementsByClassName(className);} else {var result = new Array();var elems = node.getElementsByTagName('*');for (var i = elems.length - 1; i >= 0; i--) {if (elems[i].className.indexOf(className) != -1){result[i] = elems[i];}}return result;}}
2. Operation element attributes
/** * Attribute operation */document.getAttribute('attr');document.setAttribute('attr', value);
Vi. Supplement
There are only a few DOM methods, not DOM APIs, and many other methods are not listed.