我正在使用它来创建额外的按钮。
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
var parentDIV = document.getElementById('extraDIV');
parentDIV.innerHTML = newVr;
但这会替换 parentDIV
中的现有按钮。如何添加按钮而不是替换按钮?
最佳答案
您必须使用 +
运算符来连接字符串。 operator +=
表示字符串被连接到当前值:
var newVr = "";
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
var parentDIV = document.getElementById('extraDIV');
parentDIV.innerHTML += newVr;
<div id="extraDIV">
<button>Old button</button>
<button>Old button</button>
</div>
对我有用
关于javascript - 添加动态按钮而不是替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31144992/