Add and delete simple DIV instances in JS.
Add and delete simple DIV instances in JS
Function addDiv (w, h) {// if there is a "divCell" layer, delete this layer deleteDiv (); // create a div var my = document. createElement ("divCell"); // Add it to the document page. body. appendChild (my); // specifies the position of the div through the style. If you want to set the position of the div by yourself, this sentence must exist, comment it out and you will know the effect ~ Try my. style. position = "absolute"; // specify the x coordinate through the style (random number 0 ~ 450) my. style. top = Math. round (Math. random () * 450); // specify the y coordinate through the style (random number 0 ~ 700) my. style. left = Math. round (Math. random () * 700); // specify the width of my. style. width = w; // specify the height of my. style. height = h; // specify the background color through the style. If the background image is my. style. backgroundImage = "url (img/3.jpg)" my. style. backgroundColor = "# ffffcc"; // Add the div content // my. innerHTML = I ++; // set the style to transparent my. style. filter = "alpha (opacity = 50)"; // Set ID my. id = "divCell";} function deleteDiv () {var my = document. getElementById ("divCell"); if (my! = Null) my. parentNode. removeChild (my );}
The simple example of adding and deleting a DIV in the above JS is all the content shared by xiaobian. I hope you can give us a reference and support for the help house.