Javascript 动态HTML元素创建
我动态创建了HTML画布元素,同时将元素大小的数量更改为更多,它使用以前创建的元素,而不是创建所有新对象,因此绑定也没有清除 这是我的密码Javascript 动态HTML元素创建,javascript,html,css,typescript,dynamic,Javascript,Html,Css,Typescript,Dynamic,我动态创建了HTML画布元素,同时将元素大小的数量更改为更多,它使用以前创建的元素,而不是创建所有新对象,因此绑定也没有清除 这是我的密码 <ng-container *ngFor="let i of rows; let i=index"> <ng-container *ngFor="let j of cols; let j=index"> <div id="Div{{(i * cols.length) + j}}"> &l
<ng-container *ngFor="let i of rows; let i=index">
<ng-container *ngFor="let j of cols; let j=index">
<div id="Div{{(i * cols.length) + j}}">
<canvas id="Canv{{(i * cols.length) + j}}" style="position: absolute"></canvas>
</div>
</ng-container>
</ng-container>
提前感谢。如果您不是每次都创建新元素,您会发现所有修改都是针对同一引用进行的,如下所示:
let div = document.createElement('div');
function addElement(w: number, h: number) {
div.style.backgroundColor = 'aqua';
div.style.width = w + 'px';
div.style.height = h + 'px';
document.body.appendChild(div);
}
addElement(100, 100);
addElement(20, 20);
这是一个工作版本,每次创建一个新元素
function addElement(w: number, h: number) {
const div = document.createElement('div');
div.style.backgroundColor = 'aqua';
div.style.width = w + 'px';
div.style.height = h + 'px';
document.body.appendChild(div);
}
addElement(100, 100);
addElement(20, 20);
你能把代码粘贴到这里吗?你认为我们可以在不看代码的情况下修复代码吗?你的问题需要更清楚,以及你正在处理的部分或全部代码。