【发布时间】:2019-04-05 01:24:40
【问题描述】:
我正在创建几个 HTML 按钮来充当列表,它们都被添加到具有 id 'locations' 的同一个中。每个按钮可以有不同的 id。
我可以将变量指定为 id 而不是字符串吗?
for(let i=0;(i<countryJSON.length)&&(i<10); i++){
document.getElementById("locations").innerHTML += `<button type='button' id=countrylist class="list-group-item list-group-item-action">${countryJSON[i].name}</button>`;
}
【问题讨论】:
-
你为什么认为你需要一个 id?
-
您可以随时将
i的值附加到countrylist 的id 值 -
id=countrylist${i}但确实不需要 id.... -
document.getElementById("locations").innerHTML +=对性能非常不利,而且还有其他副作用。
标签: javascript html node.js json dom