Most of the DOM methods your ' ve seen so far is useful for identifying elements. Both getElementById and Getelementbytagname allow you to quickly and easily target specific element nodes in a document. These elements can then is manipulated using methods and properties like SetAttribute or NodeValue. Thats how the image gallery works. As you can see, this is the the-the-the-majority of JavaScript work. The structure of the webpage is created with markup. JavaScript is then used to some of the details without altering the underlying structure. But it's also possible to use JavaScript to change the structure and contents of a Web page. There is some DOM methods that can alter the structure of a Web page by creating new Elementsand modifying existing ones. A briefly review a couple techniques that developers has used in the Past:document.write && INNERHTMLDOCUMENT.W Rite:the major drawback to using document.write are that it goes against the principle of unobtrusive JavaScript innerhtm L The InnerHTML property can is quite useful when you want a quick and easy-to-insert a chunk of HTML into a document. Unfortunately, InnerHTML doesnt return any references to the content you insert.
DOM MethodsAccording to the DOM, a document is a tree of nodes. If you want to add to the this tree, you need to insert new nodes. If you want to add markup to a document, you need to insert element nodes.
createelement:d ocument.createelement (nodeName) Eg:var para = document.createelement ("P");
appendchild: The simplest-to-insert a newly created node into the node tree of a document was to do it a child of an existingnod E in the document. Parent.appendchild (child); Eg:var para = document.createelement ("P"); var testdiv = document.getElementById ("Testdiv"); Testdiv.appendchild (para);
createTextNode:The node you has created is an empty paragraph element. If you want to put some text into that paragraph, you need to create a text node document.createTextNode (text); eg: var para = document.createelement ("P"); var txt = document.createtextnode ("Hello World"); Para.appendchild (TXT); var testdiv = document.getElementById ("Testdiv"); Testdiv.appendchild (para);
Inserting A new element before an existing one: There is a DOM method called
InsertBefore. You can use it to insert a new element before an existing element. Here's the Syntax:parentElement.insertBefore (newelement, targetelement) Eg:var gellery = Document.getelemen Tbyid ("Imageallery"); Gallery.parentNode.insertBefore (placeholder, gallery);
Inserting A new element after an existing one: Eg:
function InsertAfter (newelement, targetelement) { var parent = Targetelement.parentnode; if (Parent.lastchild = = targetelement) { parent.appendchild (newelement) ; Else { Parent.insertbefore (newelement, targetelement.nextsibling); }}
The next node after the target element was the NextSibling property of the target element. You can use this script code in the furture as the expandtion. Then follows the finished image gallery:/*** index.html ***/
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Image Gallery</title> <Linkrel= "stylesheet"href= "Styles/layout.css"Media= "Screen"></Head><Body> <H1>Snapshiots</H1> <ulID= "Imagegallery"> <Li> <ahref= "Images/fireworks.jpg"title= "A Fireworks display"> <imgsrc= "Images/thumbnail_fireworks.jpg"alt= "Fireworks"> </a> </Li> <Li> <ahref= "Images/coffee.jpg"title= "A Cup of black coffe"> <imgsrc= "Images/thumbnail_coffee.jpg"alt= "Coffee"> </a> </Li> <Li> <ahref= "Images/rose.jpg"title= "A red, Red Rose"> <imgsrc= "Images/thumbnail_rose.jpg"alt= "Rose"> </a> </Li> <Li> <ahref= "Images/bigben.jpg"title= "The famous Clock"> <imgsrc= "Images/thumbnail_bigben.jpg"alt= "Big Ben"> </a> </Li> </ul> <Scripttype= "Text/javascript"src= "Scripts/showpic.js"></Script></Body></HTML>
View Code
/*** Showpic.js ***/
/** * Created by Administrator on 9/9/2015.*//*can use this function to count how many children nodes the BODY element contains*/functionCountbodychildren () {varBody_element = document.getElementsByTagName ("body") [0]; alert (Body_element.nodetype); alert (body_element.childNodes.length);}functionAddloadevent (func) {varOldonload =window.onload; if(typeofWindow.onload! = ' function ') {window.onload=func; } Else{window.onload=function() {oldonload (); Func (); } }}functionInsertAfter (newelement, targetelement) {varParent =Targetelement.parentnode; if(Parent.lastchild = =targetelement) {Parent.appendchild (newelement); } Else{parent.insertbefore (newelement, targetelement.nextsibling); }}functionPrepareplaceholder () {if(!document.createelement)return false; if(!document.createtextnode)return false; if(!document.getelementbyid)return false; if(!document.getelementbyid ("Imagegallery"))return false; varplaceholder = document.createelement ("img"); Placeholder.setattribute ("id", "placeholder"); Placeholder.setattribute ("src", "images/placeholder.gif"); Placeholder.setattribute ("Alt", "My Image gallery"); varDescription = document.createelement ("P"); Description.setattribute ("id", "description"); varDesctext = document.createTextNode ("Choose an image"); Description.appendchild (Desctext); varGallery = document.getElementById ("Imagegallery"); InsertAfter (placeholder, gallery); InsertAfter (description, placeholder);}functionPreparegallery () {if(!document.getelementsbytagname)return false; if(!document.getelementbyid)return false; if(!document.getelementbyid ("Imagegallery"))return false; varGallery = document.getElementById ("Imagegallery"); varLinks = gallery.getelementsbytagname ("a"); for(vari=0; i<links.length; i++) {Links[i].onclick=function() { returnShowpic ( This) ?false:true; } }}functionShowpic (whicpic) {if(!document.getelementsbytagname)return false; if(!document.getelementbyid ("placeholder"))return false; varSource = Whicpic.getattribute ("href"); varplaceholder = document.getElementById ("placeholder"); Placeholder.setattribute ("SRC", source); if(document.getElementById ("description")) { varText = Whicpic.getattribute ("title")? Whicpic.getattribute ("title"): 3; varDescription = document.getElementById ("description"); Description.firstChild.nodeValue=text; } return true;} Addloadevent (Prepareplaceholder); addloadevent (preparegallery);
View Code
/*** Layout.css ***/
body{Font-family: "Helvetica", "Arial", serif; Color: #333; Background-color: #ccc; Margin:1em10%;} h1{Color: #333; /*background-color: #777;*/}a{color: #c60; Background-color:transparent; Font-Weight:bold; Text-Decoration:none;} ul{padding:0;} li{float: Left; Padding:1em; List-Style:none;} img {display:block; Clear:both;}
View Code
Alter the structure of Web pages with JavaScript