【发布时间】:2019-11-20 05:53:14
【问题描述】:
我正在使用 dom 创建一些 html 元素。确切地说是按钮。我对 javascript 很陌生,所以任何建议都将不胜感激。
我为此使用的 js 基于 div 的节点列表。我正在使用 for 循环创建基于节点列表中每个对象的按钮(每个 div 生成一个按钮)。
这里是js:
let indInit = function(){
for(i = 0; i < slides.length; i++){
document.querySelector('.gallery-indicator-nav').innerHTML +=
'<button class="gallery-indicator" onclick="currentSlide(1)></button>';
}
};
indInit();
显然,我最终得到了
<div class="gallery-indicator-nav">
<button class="gallery-indicator" onclick="currentSlide(1)></button>
<button class="gallery-indicator" onclick="currentSlide(1)></button>
<button class="gallery-indicator" onclick="currentSlide(1)></button>
</div>
我希望我的按钮生成如下:
<div class="gallery-indicator-nav">
<button class="gallery-indicator" onclick="currentSlide(1)></button>
<button class="gallery-indicator" onclick="currentSlide(2)></button>
<button class="gallery-indicator" onclick="currentSlide(3)></button>
</div>
我假设我需要使用某种排序或数学对象和连接,但至少可以说对谷歌的研究一直令人困惑。我认为必须有一个简单的解决方案 =)
【问题讨论】:
标签: javascript dom slider innerhtml