Posted By

mlange on 10/03/06


Tagged

DOM


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

shachi


DOM tree


 / Published in: JavaScript
 

  1. // gets the element with an id of "MG"
  2. document.getlElementById("Mg").value;
  3.  
  4. // returns all the div elements as an array
  5. document.getElementsByTagName("div");
  6.  
  7.  
  8. // returns the first one of the <p> elements in the array
  9. document.getElementsByTagName("p")[0];
  10.  
  11. // The root element in HTML <html>...</html>
  12. document.documentElement;
  13.  
  14. // Creates a new <img> object
  15. document.createElement("img");
  16.  
  17. // now this text node can be added to an element in the markup.
  18. var favshows = document.createTextNode("24 and Lost");
  19.  
  20.  
  21. /*
  22. Any change in the browser's model of a web page will automatically update the actual web page in user's browsers
  23. - divNode.parentNode
  24. - divNode.childNodes
  25. - divNode.firstChild
  26. - divNode.lastChild
  27.  
  28. node node type nodeName nodeValue
  29. div element "div" null
  30. em element "em" null
  31. "abcd" text null "abcd"
  32.  
  33. Text nodes do not have a nodeName; the node value for an element node is underfined.
  34.  
  35. Element nodes have a getAttribute() and setAttribute) method. (only elements can have attributes)
  36.  
  37. Element nodes get the parent and childNodes properties from the Node object.
  38.  
  39. If you use a property on a node where that property doesn't apply, you'll get a value like "null" or "undefined"
  40.  
  41. 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.
  42.  
  43. */
  44.  
  45.  
  46. if (someNode.nodeType == Node.ELEMENT_NODE) {
  47. ...
  48. } else if (someNode.nodeType == Node.TEXT_NODE) {
  49. ..
  50. }
  51.  
  52. /*
  53. Note tha tsome browsers don't recognize Node.ELEMENT_NODE and report an error. <em>In other words, avoid using it</em>.
  54. */

Report this snippet  

You need to login to post a comment.