JS DOM Node

Source: Internet
Author: User

HTML code:

<body onload = "loaded12 ()" ><form name= "Form1" action= "http://www.baidu.com" method= "POST" > Name: <label ><input type= "text" name= "name"/></label><br/>    Age: <label ><input type= "text" name = "Age"/></label><br/>    <input type= "Submit" name= "Submit" value= "commit" onclick= "Submitclick ()"/ >    <input type= "reset" name= "reset" value= "reset"/>    <input type= "image" Name= "image" ></form >

Traversing nodes

function loaded12 () {var txt= "", var c=document.body.childnodes;for (i=0; i<c.length; i++)  {  txt=txt + c[i]. NodeName + "-" + c[i].nodetype+ "-" +c[i].nodevalue+ "   ";  };   alert (TXT);}

Change the value of the node: The following two lines of code are equivalent

document.getElementById ("title"). Childnodes[0].nodevalue= "I am the title";
document.getElementById ("title"). Innerhtml= "I am the title";

To change a property:

document.getElementById ("title"). Childnodes[0].nodevalue= "I am the title";//The following two sentences are equivalent to document.getElementById ("title"). SetAttribute ("Style", "background:red;"); document.getElementById ("title"). style.background= "Red";

To add a node:

var tilte = document.getElementById ("title"), IMG = document.createelement ("img"), img.src= "123.png"; Title.appendchild (IMG);

JS DOM Node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.