Add and delete simple DIV instances in JS.

Source: Internet
Author: User

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.

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.