Except where otherwise noted, the contents of this presentation are © Copyright 2007 Marty Stepp and are licensed under the Creative Commons Attribution 2.5 License.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/XHTML1/DTD/XHTML1-strict.dtd"> <html xmlns="http://www.w3.org/1999/XHTML" xml:lang="en" lang="en"> <head> <title>Page Title</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="en-us" /> </head> <body> <h1>This is a heading</h1> <p>A paragraph with a <a href="http://www.google.com/">link</a>.</p> <ul> <li>a list item</li> <li>another list item</li> <li>a third list item</li> </ul> </body> </html>
<p>This is a paragraph of text with a <a href="/path/to/another/page.html">link</a> inside.
every node's DOM object has the following properties:
firstChild
, lastChild
: start/end of this node's list of childrenchildNodes
: array of all this node's childrennextSibling
, previousSibling
: neighboring nodes that have the same parentparentNode
: the element that contains this node<p id="foo">This is a paragraph of text with a <a href="/path/to/another/page.html">link</a> inside.
Every DOM node object has these methods:
appendChild(node)
: places the given node at the end of this node's child listinsertBefore(newChild, oldChild)
: places the given new node in this node's child list just before oldChild
removeChild(node)
: removes the given node from this node's child listreplaceChild(newChild, oldChild)
: replaces the given child node with the given new nodecreateElement
// create a new <h2> node var newHeading = document.createElement("h2"); newHeading.style.color = "green"; newHeading.innerHTML = "This is a heading"; // put it onto the page in the div with id "content" var contentArea = document.getElementById("content"); contentArea.appendChild(newHeading);
document.createElement("tag")
constructs a new empty DOM node representing an element of that type// in window.onload event handler, document.getElementById("thisslide").onclick = slideClick; function slideClick() { var p = document.createElement("p"); p.innerHTML = "A paragraph!"; this.appendChild(p); }
innerHTML
Why not just code the previous example this way?
// equivalent to previous slide, but worse style function slideClick() { this.innerHTML += "<p>A paragraph!</p>"; }
innerHTML
codeImagine that the new node is more complex:
function slideClick() { this.innerHTML += "<p style='color: red; " + "margin-left: 50px;' " + "onclick='myOnClick();'>" + "A paragraph!</p>"; }
"
and '
innerHTML
function slideClick() {
var p = document.createElement("p");
p.style.color = "red";
p.style.marginLeft = "50px";
p.onclick = myOnClick;
p.innerHTML = "A paragraph!"; // here innerHTML is okay
this.appendChild(p);
}
innerHTML
if a node's inner contents are trivialClick a rectangle to move it to the front. Shift-click a rectangle to delete it.
document.getElementById("id")
element.getElementsByTagName("tag")
"p"
, "div"
, etc.)document
or on a specific nodehighlight all paragraphs in document
var allParas = document.getElementsByTagName("p");
for (var i = 0; i < allParas.length; i++) {
allParas[i].style.backgroundColor = "yellow";
}
<body>
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>You get the idea...</p>
</body>
getElementById
highlight all paragraphs inside of the section with ID "footer"
var footer = document.getElementById("footer");
var footerParas = footer.getElementsByTagName("p");
for (var i = 0; i < footerParas.length; i++) {
footerParas[i].style.backgroundColor = "yellow";
}
<p>This won't be returned!</p>
<div id="footer">
<p>1234 Street</p>
<p>Atlanta, GA</p>
</div>
Every Javascript program can refer to the following global objects:
window
objectwindow
objectalert
,
blur
,
clearInterval
,
clearTimeout
,
close
,
confirm
,
focus
,
moveBy
,
moveTo
,
open
,
print
,
prompt
,
resizeBy
,
resizeTo
,
scrollBy
,
scrollTo
,
setInterval
,
setTimeout
navigator
objectscreen
objecthistory
objectlocation
objectdocument
object