javascript - 添加动态按钮而不是替换

标签 javascript jquery

我正在使用它来创建额外的按钮。

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/

相关文章:

javascript - Greasemonkey 代码不起作用

javascript - 将 jQuery 中的元素隐藏在表格内

javascript - 重新加载 div 后,jQuery 悬停功能不起作用

javascript - 将 2 个相关数组映射在一起

jquery 需要 focusout() 的替代方案

javascript - 如何阻止任何 slider 向 URL 添加主题标签

javascript - 如何定义变量以在 JQuery 和 javascript 之间传递数据?

javascript - 关于 javascript 中的 Object.create 的困惑

java - 我可以列出一个只有 Javascript/JQuery 的目录吗?

javascript - 将自定义数据传递给 React 中的 PrivateRoute 组件