js部分
<script>
var span = document.querySelectorAll("sapn"); //获取所有span元素
for (var i = 0; i < span.length; i++) {
span[i].index = i; //为元素下标赋值
span[i].addEventListener("click", function() { //给所有span元素添加点击事件
console.log(this.index);
})
}
</script>
html部分
<body>
<div>div
<p>p1
<sapn>span1</sapn>
<sapn>span2</sapn>
<sapn>span3</sapn>
<sapn>span4</sapn>
</p>
</div>
</body>
逐个点击效果