<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
ul {
list-style: none;
}
input {
width: 30px;
height: 30px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<ul>
<li>
<input type="button" name="" id="" value="-">
<b>0</b>
<input type="button" name="" id="" value="+">
单价: <i>23.3元</i>
小计: <span>0元</span>
</li>
<li>
<input type="button" name="" id="" value="-">
<b>0</b>
<input type="button" name="" id="" value="+">
单价: <i>52.3元</i>
小计: <span>0元</span>
</li>
<li>
<input type="button" name="" id="" value="-">
<b>0</b>
<input type="button" name="" id="" value="+">
单价: <i>45.3元</i>
小计: <span>0元</span>
</li>
</ul>
<script>
var oli = document.getElementsByTagName('li');
for (i = 0; i < oli.length; i++) {
fun(oli[i]);
}
function fun(list) {
var btn = list.getElementsByTagName('input');
var b = list.getElementsByTagName('b')[0];
var i=list.getElementsByTagName('i')[0];
var span=list.getElementsByTagName('span')[0];
var number=parseFloat(b.innerHTML);
var sum=0;
btn[0].onclick=function(){
if(number>0){
number--;
sum-=parseFloat(i.innerHTML);
}
b.innerHTML=number;
span.innerHTML=parseFloat(i.innerHTML)*number+'元';
}
btn[1].onclick = function () {
number++;
b.innerHTML = number;
span.innerHTML = parseFloat(i.innerHTML) * number + "元"
}
}
</script>
</body>
</html>
效果如图