js for循环取值,alert(i)始终是3,怎么才能让button分别显示1,2,3,function(){}怎么才能取到对应的i值
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>abc</title></head><body><inputc...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>abc</title>
</head>
<body>
<input class="btn" type="button" />
<input class="btn" type="button" />
<input class="btn" type="button" />
<script>
for(var i=0;i<3;i++){
document.getElementsByClassName("btn").item(i).onmousedown=function(){
alert(i);
};
}
</script>
</body>
</html> 展开
<html>
<head>
<meta charset="utf-8">
<title>abc</title>
</head>
<body>
<input class="btn" type="button" />
<input class="btn" type="button" />
<input class="btn" type="button" />
<script>
for(var i=0;i<3;i++){
document.getElementsByClassName("btn").item(i).onmousedown=function(){
alert(i);
};
}
</script>
</body>
</html> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询