Jump to content

Document


Drycodez
 Share

Recommended Posts

If i wish to walk through the document node tree, how can this be done? HTML:

<body><p>some text</p></body>

JS:

document.firstChild.firstChild.innerHTML="i have changed the p element text!";

why is the above code not working?

Edited by O. Samuel
Link to comment
Share on other sites

When opening a document as an HTML one, it implicitly adds required elements like <html> and <title>. You need to make sure that either you have a valid HTML document or you serve it as an XML document to prevent these additions.Also, whitespace is counted as a node, so even if the document was served as XML, "document.firstChild.firstChild" would match the first whitespace of the body element. Right now, it's probably matching the whitespace of the implicitly added HTML element, and inherently doesn't work because having something other than one head and one body is invalid.

Link to comment
Share on other sites

When opening a document as an HTML one, it implicitly adds required elements like <html> and <title>. You need to make sure that either you have a valid HTML document or you serve it as an XML document to prevent these additions.Also, whitespace is counted as a node, so even if the document was served as XML, "document.firstChild.firstChild" would match the first whitespace of the body element...
thanks!
Link to comment
Share on other sites

document.lastChild will not refer to the last element in the body, if that's what you're thinking. As with people, a child is an immediate descendant. The last child of document should be the <html> element. (I suppose it could be a text node, also.)

Edited by Deirdre's Dad
Link to comment
Share on other sites

document.lastChild will not refer to the last element in the body, if that's what you're thinking. As with people, a child is an immediate descendant. The last child of document should be the <html>...
hmmm, kinda confusing! i thought the last child of a document is the <body> tag! What do you mean? Plz make me understand!
Link to comment
Share on other sites

Assuming the document is well formed, the <body> element is the last element contained by the <html> element. The document itself contains (1) the <!DOCTYPE> element and (2) the <html> element.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Why don't you figure out what elements you're trying to reference? Use Firebug and console.log to examine the elements you're working with and then you can figure out why it doesn't do what you expect. You can look at the entire DOM tree through Firebug.

Link to comment
Share on other sites

Why don't you figure out what elements you're trying to reference? Use Firebug and console.log to examine the elements you're working with and then you can figure out why it doesn't do what you expect. You can look at the entire DOM tree through Firebug.
ok, thanks!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...