ChildNodesProperty:the ChildNodes Property is a A-getting information about the children of any element in a document ' s node t Ree. It returns an array containing all the children of an element node:element.childNodes; Lets say you wanted to get all the children of the BODY element. var body_element = document.getElementsByTagName ("body") [0]; To access the children of the BODY element, you just need to use:body_element.childNodes; You could write a function to find out how many elements the BODY element contains:
function Countbodychildren () { var body_element = document.getElementsByTagName ("body") [0]; alert (body_element.childNodes.length); }
If the want this function is excuted when the page is loads, you can use the OnLoad event Handlerto does this. Window.onload = Countbodychildren; When the document loads, the Countbodychildren function would be invoked.
NodeTypeProperty:this would tell us exactly what kind of the node we ' re dealing with. The NodeType property was called with the following syntax:node.nodeTypeinstead of returning a string like "element" or " Attribute ", it returns a number. There is possible values for NodeType, and only 3 of them is going to being of much practical use:
- Element nodes has a nodeType value of 1
- Attribute nodes has a nodeType value of 2
- Text nodes has a NodeType value of 3
NodeValueProperty:if want to change the value of a text node, there are a DOM property called NodeValue so can be used to get (and set) the value of a node:node.nodeValue firstchild and lastchild property:node.firstChild = = Node.childnodes[0]n Ode.lastchild = = Node.childnodes[node.childnodes.length-1] This image gallery projects is as FOLLWS:/*** index.html ***/
<! DOCTYPE html>//alert (description.childNodes.length);//window.onload = Countbodychildren;//var description = document.getElementById ("description");//alert (description.childnodes[2].nodevalue);</script></body>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);}functionShowpic (whicpic) {varSource = Whicpic.getattribute ("href"); varText = Whicpic.getattribute ("title"); varplaceholder = document.getElementById ("placeholder"); varimg = Placeholder.getelementsbytagname ("img") [0]; Img.setattribute ("SRC", source); varDescription = document.getElementById ("description"); varDesc_p = Description.getelementsbytagname ("P") [0]; Desc_p.firstChild.nodeValue=text;}
View Code/*** Layout.css ***/
Body{font-family:"Helvetica", "Arial", serif;Color:#333;Background-color:#ccc;margin:1em 10%;}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 CodeThe structure is like the pic shows below:
A JavaScript Image Gallery