使用appendChild添加div

时间:2016-10-20 18:08:26

标签: javascript html css

这段代码应该是循环并添加多个div,但它不起作用。单击它时,只显示一个div。如果我再次点击,没有任何反应。

if (EasyPermissions.hasPermissions(Splash.this, perms )) {...
 } else if (EasyPermissions.somePermissionPermanentlyDenied(Splash.this, permsList)) {

 } else {
    EasyPermissions.requestPermissions(Splash.this, "Without these permissions, the app is unable to successfully complete authentication. Please allow us to access your device so that we can better serve you. "  ,PERMS_REQUEST_CODE, perms );
 }

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

Your code does not work because you did not do anything with the variable "i" in the for statement. If you look at the fiddles of user2181397 & meghan Armes you will see how they added a line in the script to put it to work.

I tested the below in my IDE and it works just fine:

<body>
   <div class="start" style="margin-top:50px; color:black;">
      <div id = "coba">
        <p>Click Me</p>
      </div>

      <div id = "cobi">
      </div>
 </div>

<script>
var divs = document.getElementById("coba").addEventListener("click", function() {

for (var i = 1; i < 100; i++) {
   var di = document.createElement('div');
   di.innerHTML=i;
   document.getElementById('coba').appendChild(di);

}
});

</script>
</body>