Java Reference
In-Depth Information
Figure 12-10
Let's recap. You created a text node and stored it in the newText variable. You created an <h1/> element
and stored it in the newElem variable. Then you appended the text node as a child node to the <h1/>
element. That still leaves you with a problem: You've created an element with a value, but the element
isn't part of your document. You need to attach the entirety of what you've created so far to the docu-
ment body. Again, you can do this with the appendChild() method, but this time supply it to the
document.body object (which, too, is a Node ).
document.body.appendChild(newElem);
This completes the fi rst part of your code. Now all you have to do is repeat the process for the <p/>
element.
newText = document.createTextNode(“This is some text in a paragraph”);
newElem = document.createElement(“p”);
newElem.appendChild(newText);
document.body.appendChild(newElem);
You create a text node fi rst; then you create an element. You attach the text to the element, and fi nally
you attach the element and text to the body of the document.
Search WWH ::




Custom Search