我的:
class CountInfo {
constructor() {
let tfoot = document.getElementsByTagName("tfoot")[0]
let countInfoTag = tfoot.children[0].children[1]
let tmp = countInfoTag.innerHTML.match(/\d+/g)
let countInfo = {
price: 0,
num: 0
}
countInfo.price = (tmp[0] - 0) + (tmp[1] - 0) * 0.01
countInfo.num = tmp[2] - 0
this.countInfoTag = countInfoTag
this.countInfo = countInfo
}
delete(price) {
this.countInfo.num--
this.countInfo.price = this.countInfo.price - price
this.countInfoTag.innerHTML = `${this.countInfo.price.toFixed(2)}(${this.countInfo.num}件商品)`
}
add(price) {
this.countInfo.num++
this.countInfo.price = this.countInfo.price + price
this.countInfoTag.innerHTML = `${this.countInfo.price.toFixed(2)}(${this.countInfo.num}件商品)`
}
}
bind()
function add(items) {
let tr = document.createElement("tr")
tr.innerHTML = `<td>${items.name}</td><td>${items.price.toFixed(2)}</td><td><a href="javascript:void(0);">删除</a></td>`
let tbody = document.getElementsByTagName("tbody")[0]
tbody.appendChild(tr)
tr.children[2].children[0].onclick = click
const countInfo = new CountInfo()
countInfo.add(items.price)
}
function bind() {
let tags = document.getElementsByTagName("a")
const LEN = tags.length
for (let i = 0; i < LEN; i++) {
tags[i].onclick = click
}
}
function click(e) {
const countInfo = new CountInfo()
let tr = e.srcElement.parentElement.parentElement
countInfo.delete(tr.children[1].innerHTML - 0)
tr.remove()
}