首页 前端知识 JS | 获取当前点击的是第几个元素

JS | 获取当前点击的是第几个元素

2024-01-25 11:01:44 前端知识 前端哥 70 431 我要收藏

使用index`

<body>
<div id="main">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
</div>
</body>
<script>
window.onload = function(){
var inputs = document.getElementsByClassName("check-box");
for(let i=0;i<inputs.length;i++){
inputs[i].setAttribute("index",i);
inputs[i].onclick = function(){
console.log("第"+this.getAttribute("index")+"个");
}
}
}
</script>
复制
转载请注明出处或者链接地址:https://www.qianduange.cn//article/328.html
标签
jqueryhtml5
评论
还可以输入200
共0条数据,当前/页
发布的文章
大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!