/ Published in: JavaScript
Expand |
Embed | Plain Text
// gets the element with an id of "MG" document.getlElementById("Mg").value; // returns all the div elements as an array document.getElementsByTagName("div"); // returns the first one of the <p> elements in the array document.getElementsByTagName("p")[0]; // The root element in HTML <html>...</html> document.documentElement; // Creates a new <img> object document.createElement("img"); // now this text node can be added to an element in the markup. var favshows = document.createTextNode("24 and Lost"); /* Any change in the browser's model of a web page will automatically update the actual web page in user's browsers - divNode.parentNode - divNode.childNodes - divNode.firstChild - divNode.lastChild node node type nodeName nodeValue div element "div" null em element "em" null "abcd" text null "abcd" Text nodes do not have a nodeName; the node value for an element node is underfined. Element nodes have a getAttribute() and setAttribute) method. (only elements can have attributes) Element nodes get the parent and childNodes properties from the Node object. If you use a property on a node where that property doesn't apply, you'll get a value like "null" or "undefined" Every node has a property called nodeType, along with nodeName and nodeValue. The nodeType property returns a number that maps to a value stored in the Node class. */ if (someNode.nodeType == Node.ELEMENT_NODE) { ... } else if (someNode.nodeType == Node.TEXT_NODE) { .. } /* Note tha tsome browsers don't recognize Node.ELEMENT_NODE and report an error. <em>In other words, avoid using it</em>. */
You need to login to post a comment.
