简体   繁体   中英

Whenever I clicked on a button a new div is created

When the button is clicked, a new div is added. Each time it is clicked an additional div is added. The div have following properties:

  1. It has a class box
  2. For the text inside the div, number the div starting with 1.(Keep a counter variable to track what number comes next.)

I hope these pic might help you:

在此处输入图像描述

HTML:

<button onclick="newDiv()">Add Box</button>
<div id="parent"></div>

JS:

function newDiv() {
  var divElem = document.createElement("div");
    elementAttr.setAttribute('class','divclass');
//if you want to add text inside
    //var imText = document.createTextNode("Hello Wolrd");
   // divElem.appendChild(imText);
//you need a parent Div to put this inside
    var parent = document.getElementById("parent");
    parent.appendChild(divElem);}

just make a css class with a style that you want so when a div is made it will get a style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM