简体   繁体   English

每当我单击按钮时,都会创建一个新的 div

[英]Whenever I clicked on a button a new div is created

When the button is clicked, a new div is added.单击该按钮时,将添加一个新的 div。 Each time it is clicked an additional div is added.每次单击它时都会添加一个额外的 div。 The div have following properties: div 具有以下属性:

  1. It has a class box它有一个 class 盒子
  2. For the text inside the div, number the div starting with 1.(Keep a counter variable to track what number comes next.)对于 div 内的文本,从 1 开始为 div 编号。(保留一个计数器变量以跟踪下一个数字。)

I hope these pic might help you:我希望这些图片可以帮助你:

在此处输入图像描述

HTML: 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.只需制作一个带有您想要的样式的 css class,这样当制作一个 div 时,它就会获得一个样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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