-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_shoppingCart.html
116 lines (112 loc) · 2.75 KB
/
3_shoppingCart.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE HTML>
<html>
<head>
<title>使用Selector API实现购物车客户端计算</title>
<meta charset="utf-8" />
<style>
table{width:600px; text-align:center;
border-collapse:collapse;
}
td,th{border:1px solid black}
td[colspan="3"]{text-align:right;}
</style>
</head>
<body>
<table id="data">
<thead>
<tr>
<th>商品名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
</tr>
</thead>
<tbody>
<tr>
<td>iPhone6</td>
<td>¥4488.00</td>
<td>
<button>-</button>
<span>1</span>
<button>+</button>
</td>
<td>¥4488.00</td>
</tr>
<tr>
<td>iPhone6 plus</td>
<td>¥5288.00</td>
<td>
<button>-</button>
<span>1</span>
<button>+</button>
</td>
<td>¥5288.00</td>
</tr>
<tr>
<td>iPad Air 2</td>
<td>¥4288.00</td>
<td>
<button>-</button>
<span>1</span>
<button>+</button>
</td>
<td>¥4288.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Total: </td>
<td>¥14064.00</td>
</tr>
</tfoot>
</table>
<script>
//查找
window.onload=function(){
var table=document.getElementById("data")
var btns=document.getElementsByTagName("button")
/*function gettotal(){
var total=0
var tbody_tr=table.getElementsByTagName("tbody")[0].getElementsByTagName("tr")
for(var elem of tbody_tr){
total+=parseInt(elem.lastElementChild.innerText.slice(1))
}
table.getElementsByTagName("tfoot")[0].getElementsByTagName("td")[1].innerText= "¥" +total.toFixed(2)
console.log(tbody_tr )
}*/
//遍历每一个按钮
for(var btn of btns){
//在处理函数中应该用this关键字获得将来触发事件的当前元素本身
btn.onclick=function(){
var btn=this
// this.innerHTML="❀"
var span=this.parentNode.children[1]
// span.innerHTML="❀"
var n=parseInt(span.innerHTML)
//实现+-
if(this.innerHTML=="+"){
n++
}else if(n>1){
n--
}
span.innerHTML=n
//计算小计=单价*数量
var price=parseFloat(this.parentNode.previousElementSibling.innerHTML.slice(1))//去掉开头的¥
var subtotal=price*n
this.parentNode.nextElementSibling.innerHTML=`$${subtotal.toFixed(2)}`
//gettotal()
var tds=table.querySelectorAll("tbody>tr>td:last-child")
console.log(tds)
var total=0
for(var td of tds){
total+=parseFloat(td.innerHTML.slice(1))
console.log(total)
}
table.querySelector("tfoot>tr>td:last-child").innerHTML="¥"+ total.toFixed(2)
console.log(total)
}
}
}
</script>
</body>
</html>