Java Reference
In-Depth Information
<body>
<h1 id=”heading1”>My Heading</h1>
<p id=”paragraph1”>This is some text in a paragraph</p>
</body>
</html>
Now you can use the getElementById() method to return a reference to any of the HTML elements
with id attributes on your page. For example, if you add the following code in the shaded section, you
can fi nd and reference the <h1/> element:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<title>example</title>
</head>
<body>
<h1 id=”heading1”>My Heading</h1>
<p id=”paragraph1”>This is some text in a paragraph</p>
<script type=”text/javascript”>
alert(document.getElementById(“heading1”));
</script>
</body>
</html>
Figure 12-4 shows the result of this code in Firefox.
Figure 12-4
HTMLHeadingElement is an object of the HTML DOM. All HTML elements have a corresponding
reference type in the DOM. See Appendix C for more objects of the HTML DOM.
You might have been expecting it to return something along the lines of <h1/> or <h1 id=”heading1”> ,
but all it's actually returning is a reference to the <h1/> element. This reference to the <h1/> element is
Search WWH ::




Custom Search