4 回答
TA贡献51条经验 获得超28个赞
作用域问题
(function(n){
addd[n].onclick=function(){
var j = document.getElementsByClassName("count-input");
var h = parseInt(j[n].value)
j[n].value=h+1;
}
})(n)
加个闭包就行了
TA贡献1条经验 获得超0个赞
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.count{
width: 90px;
}
.reduce{
width: 17px;
height: 23px;
background-color: gainsboro;
float: left;
border: 1px solid #e5e5e5;
line-height: 23px;
}
.count-input{
width: 50px;
height: 15px;
float: left;
text-align: center;
border: 1px solid #aaa;
color: #343434;
padding: 4px 0;
}
.add{
width: 17px;
height: 23px;
background-color: gainsboro;
float: left;
text-align: center;
line-height: 23px;
border: 1px solid #e5e5e5;
}
</style>
</head>
<body>
<table>
<tr >
<td class="count">
<span class="reduce"></span>
<input class="count-input" value="1"/>
<span class="add">+</span>
</td>
</tr>
<tr >
<td class="count">
<span class="reduce"></span>
<input class="count-input" value="1"/>
<span class="add">+</span>
</td>
</tr>
</table>
<script type="text/javascript">
var addd = document.getElementsByClassName("add");
for(var n=0;n<addd.length;n++){
addd[n].onclick=function(){
var j = document.getElementsByClassName("count-input");
var h = parseInt(j[n].value)
j[n].value=h+1;
}}
</script>
</body>
</html>
添加回答
举报