【发布时间】:2019-10-20 13:41:41
【问题描述】:
我正在努力在单击时选择数组内元素的特定索引。这是代码的和平,提前感谢!
<div>
<span class="dot"></span>
<span class="dot"></span> -- lets say we click on this span
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
var dots = document.querySelectorAll(".dot");
for(var i = 0; i < dots.length; i ++){
dots[i].addEventListener("click", function(){
console.log(this) -- prints the actual html <span class="dot"></span>
// ?? How to print index of an element? To be more specific, in this case
just "1" ??
})
}
【问题讨论】:
标签: javascript html arrays indexof