//DOM's CRUD//C Creating Create//1. Add tags directly to the body (can be any type)document.write (' HelloWorld ');d Ocument.write (' <p> ' HelloWorld ' </p> ');//2. Create a new label and insert it into any of the tags in the body appendchildvardiv = document.createelement (' div ');d Iv.style.background= ' Red ';d iv.style.width= ' 500px ';d iv.style.height= ' 300px ';//add to Parent tagDocument.body.appendChild (div);//insert a picture into the Divvarimg = document.createelement (' img ')); Img.src= ' Image/img_02.jpg ';d Iv.appendchild (IMG);//get the P tag .varIMG1 = document.createelement (' img '); Img1.src= ' Image/img_03.jpg ';varp = document.getElementById (' word ');p. appendchild (IMG1);//There are 3 ways to delete: 1. Delete directly with body, but only for direct sub-tags//2. Get the parent tag of the current tag, and then delete//3. Directly take the current tag and call the Remove () method.Document.body.removeChild (p);//get the parent tag of the current tag, and then deleteP.parentnode.removechild (p);3P.remove ();//change to the corresponding tag, make the changes ...//Check//The first way:Document.getelementsbyclassname (); Document.getelementsbyname (); document.getElementsByTagName (); document.getElementById ();//(note: The ID returns a value, the other is the return array)//the second way: traverse the contents of the tag insidefind (Document.body); functionfind (object) { for(varIinchobject) {Console.log (object[i]); }}console.log (document.body.childNodes);
DOM crud Additions and deletions