使用angular2在div内动态创建多个div [英] Create multiple divs dynamically inside a div using angular2

查看:548
本文介绍了使用angular2在div内动态创建多个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试在div中创建多个div,但它不起作用。

有没有办法使用Angular2动态创建多个div?



我的尝试:



I have tried creating multiple divs inside div,but its not working.
Is there any way to create multiple divs dynamically using Angular2?

What I have tried:





for (var x = 0; x < 9; x++) {
           var board = document.createElement('div');
           board.className = "containernew";
           var container = document.getElementById('container');
           container.appendChild(board);
       }

推荐答案

看起来找我:



Angular:Div Fiddle - JSFiddle [ ^ ]






.container-new{
    background-color:forestgreen;
    height:50px;
    width:50px;
    display:block;
    float:left;     
    margin-left:30px;
    margin-top:30px;
    padding:10%;
    visibility:visible;
   color:red;
    
}
.projectcontainer{
    background-color:lightsteelblue;    
    display:block;
    height:500px;
    width:600px;
    margin-top:5%;
    visibility:visible;

    
}




var maindiv = document.getElementById('container');
       for (let x = 0; x < 9; x++) {
           var newdiv = document.createElement('div');
           newdiv.innerHTML = "addd";
           //newdiv.className = "container-new";
          // newdiv.setAttribute('className', 'container-new');
          newdiv.setAttribute('class', 'container-new');
           maindiv.setAttribute('class', 'projectcontainer');
           maindiv.appendChild(newdiv);
       }





i尝试了这个......我得到了newdiv的mandiv的innerhtml和cssclass。

但是孩子div的cssclass即newdiv没有得到(.container-new)..



i tried this..am getting the newdiv's innerhtml and cssclass of maindiv .
But the cssclass of child div i.e newdiv isn't getting ( .container-new)..


这篇关于使用angular2在div内动态创建多个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆