100 likes | 293 Views
Lecture 13: The Document Tree. Create Element, Remove Child. The Document Tree. Document. Root Element <html>. Element <head>. Element <body>. Element <title>. Element <a>. Element <h1>. Text: HelloWorld. Attribute “ href ”. Dynamic HTML.
E N D
Lecture 13: The Document Tree Create Element, Remove Child
The Document Tree Document Root Element <html> Element <head> Element <body> Element <title> Element <a> Element <h1> Text: HelloWorld Attribute “href”
Dynamic HTML • JavaScript can change all HTML elements dynamically • Can change all the HTML attributes • Can change all the CSS styles • Can remover exiting HTML elements and attributes • Can add new HTML elements and attributes • Can react to all existing HTML events • Can Create new HTML events
Node Object Node Document Node Element Node <body>,<div>, <p> Text Node data
Node Object • Some nodes of the tree are JavaScript objects corresponding to HTML elements. • <body>, <a>, <p> elements • Element node • Other nodes may contain text representing the content of an element. • Text node • There is a node representing the document. • Document node
Properties of Node • nodeType: Number representing the type of node(Element) • nodeName: string providing a name of this Node Type. • parentNode: reference to object that is this node’s parent. • childNodes: reference to nodes that are this node’s child. • previousSibling: previous sibling of this node. • nextSibling: next sibling of this node. • attributes: an array containing Attr instances representing this node’s attributes.
Document Node • Document Node is the parent of the html Element node. • Properties of the document object • title, body, url, etc • Methods of the document object • createElement(string): given an element type name (such as div), returns an Element instance corresponding to that element type. • createTextNode(string): return a Text instance containing the given string as its data value. • getElementById(string). • getElementsByTagName(string).
ElementNode • Element node represents HTML elements. • <a>, <p>, <body>, etc • Important methods of Element nodes. • getAttribute(String) • setAttribute(String, String) • removeAttribute(String) • hasAttribute(String)
Text Node • Instances of the text DOM object are used to represent character data. • The primary property of Text node is data, which is the text represented by the Text node. • One Element node may have several Text nodes that are siblings of a single Text node.