Document Object Model (Dom) Api For Javascript: Al and Range
Document Object Model (Dom) Api For Javascript: Al and Range
Document Object Model (Dom) Api For Javascript: Al and Range
Example
The following JavaScript lists all the nodes in the <body> section, in a
depth-first search manner, via a recursive function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DOM Tree</title>
<script>
function printNode(node) {
document.writeln("Node name=" + node.nodeName + ", value=" +
node.nodeValue + ", type=" + node.nodeType + "<br>");
if (node.hasChildNodes()) {
var childs = node.childNodes;
for (var i = 0; i < childs.length; i++) {
printNode(childs[i]);
}
}
}
</script>
</head>
<body onload="printNode(document.body)"><h2
onmouseover="this.style.color='red'"
onmouseout="this.style.color=''">Testing</h2><p>welcome to
<i>JavaScript</i>...</p></body>
</html>
Node name=BODY, value=null, type=1
Node name=H2, value=null, type=1
Node name=#text, value=Testing, type=3
Node name=P, value=null, type=1
Node name=#text, value=welcome to , type=3
Node name=I, value=null, type=1
Node name=#text, value=JavaScript, type=3
Node name=#text, value=..., type=3
Accessing the HTML element via Node interface may not be too useful
nor practical for JavaScript applications, as you need to know the actual
topological structure of the DOM-tree. Furthermore, some browsers
(e.g., firefox) may create extra Text nodes to contain the white spaces
between tags.
Text Node
DOM models the texts enclosed by HTML tags as a separate text node.
It cannot have child node. To retrieve the text content, you could the
property nodeValue. For example,
<p id="magic">Hello</p>
......
console.log(document.getElementById("magic").firstChild.nodeValue
);
document.getElementById("magic").firstChild.nodeValue = "Hi"; //
text change to "Hi"
Attribute Properties
To access an attribute of an Element node called elementName, you
could either use:
property elementName.attributeName, where attributeName is the
name of the attribute, or
methods elementName.getAttribute(name) and elementName.setAttri
bute(name, value).
For example,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test Attributes</title>
</head>
<body>
<p id="magic1" align="left">Hello</p>
<p id="magic2" align="center">Hello, again.</p>
<script type=text/javascript>
var node = document.getElementById("magic1");
document.writeln(node.align); // Get attribute "align"
node.align = "center"; // Set attribute "align" to a new value
node = document.getElementById("magic2");
document.writeln(node.getAttribute("align")); // Read attribute
"align"
node.setAttribute("align", "right"); // Write attribute "align"
</script>
</body>
</html>
Attribute style (for CSS)
Element has a property called style, which models CSS style with CSS
properties such as color and textAlign. For example,
<p id="magic">Hello</p>
......
document.getElementById("magic1").style.color="green";
document.getElementById("magic1").style.textAlign="right";
JavaScripts are often event-driven. That is, a piece of codes (called event
handler) fire in response to a certain user's or browser's action, such as
clicking a button, enter some texts, or loaded a page.
DOM API provides methods for capturing events so you can perform
your own actions in response to them. It also provides an Event object
which contains information specific to a given event that can be used by
your event handler.
Built-in Events and Event Handlers