使用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)..