Professional Documents
Culture Documents
CH 10-2
CH 10-2
JavaScript Group
Department of Information Technology Supporting and Maintenance
Reference Book
• Section 1 • Section 4
What is JavaScript? Document Object Model(DOM)
JavaScript in HTML DOM Extensions
• Section 5
Variables, Scope, and Memory
Events, Forms and Canvas
• Section 2 • Section 6
Language Basics Client-side Storage
Reference Types
• Section 3
Understanding Objects
Window Object
Outlines
• What is DOM?
• Hierarchy of Nodes
• Node Type
• Node Relationships
• Document Object
• Manipulating Nodes
DOM Standard Attributes
eg. In Html
<body>
<div id= “mydiv” class=“bd” title=”Body Text” lang=”en” dir= “ltr”>
Hello!</div>
</body>
DOM Standard Attributes(Cont’d)
Using DOM
<body>
<script type=”text/javascript”>
var div=document.createElement(“div”);
div.id= “mydiv”;
div.classname= “bd”;
div.title=”Body Text”;
div.lang=”en”;
div.dir=”ltr”;
div.appendChild(document.createTextNode(“Hello!”));
document.body.appendChild(div);
</script> </body>
Getting Attribute
Example:
In HTML :
<div id=”myDiv” class=”bd” title=”Body text” lang=”en” dir=”ltr”></div>
All of the information specified by this element may be retrieved using the
following JavaScript code:
var div = document.getElementById(“myDiv”);
alert(div.id); //”myDiv”
alert(div.className); //”bd”
alert(div.title); //”Body text”
alert(div.lang); //”en”
alert(div.dir); //”ltr”
Getting Attributes (Cont’d)
div.setAttribute(“id”, “someOtherId”);
div.setAttribute(“class”, “ft”);
div.setAttribute(“lang”,”fr”);
div.setAttribute(“dir”, “rtl”);
Remove Attributes
div.removeAttribute(“class”);
div.removeAttribute(“id”);
Changing Attribute Value
to change each of the attributes by assigning new values to the properties:
div.id = “someOtherId”;
div.className = “ft”;
div.title = “Some other text”;
div.lang = “fr”;
div.dir =”rtl”;
Manipulating Nodes
appendChild( ): add an HTML Element ( accept one argument: new node, add a node to end
of childNodes list, return new node)
insertBefore( ): accept two arguments: node to insert and node to reference, insert new node
before reference node, return new node
replaceChild( ): replace an HTML Element ( accept two arguments: the node to insert and the
node to replace, return node to replace)
removeChild( ): remove an HTML element (accept one argument: node to remove, return
this node)
normalize( ): joined into a single text node
splitText( ) :splits a text node into two text nodes
appendChild( )
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script type=“text/javascript”>
var para = document.createElement("p");
var node = document.createTextNode("This is new.");
para.appendChild(node);
var element = document.getElementById("div1");
element.appendChild(para);
</script>
insertBefore( )
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script type=“text/javascript”>
var para = document.createElement("p");
var node = document.createTextNode("This is new.");
para.appendChild(node);
var element = document.getElementById("div1");
var child = document.getElementById("p1");
element.insertBefore(para, child);
</script>
replaceChild( )
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script type=“text/javascript”>
var para = document.createElement("p");
var node = document.createTextNode("This is new.");
para.appendChild(node);
var parent = document.getElementById("div1");
var child = document.getElementById("p1");
parent.replaceChild(para, child);
</script>
removeChild( )
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script type=“text/javascript”>
var parent = document.getElementById("div1");
var child = document.getElementById("p1");
parent.removeChild(child);
</script>
normalize( ) Example:
Example:
var element = document.createElement("div");
element.className ="message";
var textNode = document.createTextNode("Hello world!");
element.appendChild(textNode);
var anotherTextNode = document.createTextNode("welcome");
element.appendChild(anotherTextNode);
document.body.appendChild(element);
alert(element.childNodes.length); //2
element.normalize();
alert(element.childNodes.length); //1
alert(element.firstChild.nodeValue); //Hello world!welcome
splitText( ) Example:
Example:
var element = document.createElement("div");
element.className = "message";
var textNode = document.createTextNode("Hello world!");
element.appendChild(textNode);
document.body.appendChild(element);
var newNode = element.firstChild.splitText(5);
alert(element.firstChild.nodeValue); //“Hello"
alert(newNode.nodeValue); //“ world!"
alert(element.childNodes.length); //2
Manipulating Tables
2. Find an element by its id. #replace-text div inside this box: Set the text color to
#777. Set the font size to 1rem. Set the replace content to “I can do
<em>whatever</em> I want with JavaScript.”
University of Computer Studies
Thank you!