// 获取所有加号按钮 var addButtons = document.getElementsByName('add') for (var i = 0; i < addButtons.length; i++) { // 因为获取的按钮是一个集合,因此需要取出每一项来添加事件 var addButton = addButtons[i] // 为每个按钮添加点击事件,传入参数event addButton.onclick = function (event) {} }
接下来要做的就是获取输入框,并改变其值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 获取当前点击的按钮 var targetButton = event.target // 操作与之关联的input元素 (存在空白节点问题,所以需要连续向上取两次) var inputElement = targetButton.previousSibling.previousSibling // 获取数量(string类型需要转化为Number类型) var count = Number(inputElement.value) // 计算 count += 1 // 将值更新到页面 inputElement.value = count // 获取减按钮(这里需要判断减按钮是否可以被点击) var minuButton = inputElement.previousSibling.previousSibling if (count > 1) { minuButton.removeAttribute('disabled') } else { minuButton.setAttribute('disabled', 'disabled') }
// 删除按钮 var delButtons = document.getElementsByName("delete") for (var i = 0; i < delButtons.length; i++) { var delButton = delButtons[i] delButton.onclick = function (event) { var targetButton = event.target var currentTrElement = targetButton.parentNode.parentNode var parentElement = currentTrElement.parentNode parentElement.removeChild(currentTrElement) //修改上方总数量 var numberElement = document.getElementById('number') var number = parseInt(numberElement.textContent) number -= 1 numberElement.textContent = number /* 阻止a标签跳转 1. 通过调用调用 preventDefault() 2. 时间的处理函数 return 语句 -> return false */ event.preventDefault() } }
var checkall = document.getElementById("checkall") checkall.onclick = function () { console.log(1) console.log(checkall.checked) var checkElements = document.getElementsByName("check") var cart = document.getElementById('cart') var trElements = cart.getElementsByTagName('tr') if (checkall.checked) { for (var i = 0; i < checkElements.length; i++) { var checkElement = checkElements[i] checkElement.setAttribute("checked", "checked") } for (var i = 1; i < trElements.length; i++) { var trElement = trElements[i] trElement.style.backgroundColor = '#fff4e8' }
} else { for (var i = 0; i < checkElements.length; i++) { var checkElement = checkElements[i] checkElement.removeAttribute('checked') } for (var i = 1; i < trElements.length; i++) { var trElement = trElements[i] trElement.style.backgroundColor = 'transparent' } }
for (var i = 0; i < addButtons.length; i++) { var addButton = addButtons[i] // 绑定鼠标事件 // 修改数量 addButton.onclick = function (event) { console.log('click') /* 通过event对象获取用户点击的是哪个按钮 * 绑定事件的元素 -> 所有的<bottonname='add'>+</botton> * 触发事件的元素 -> 只能有一个 */ // console.log(event.currentTarget, event.target, this) var targetButton = event.target // 操作与之关联的input元素 var inputElement = targetButton.previousSibling.previousSibling // console.log(inputElement) // 获取数量(string类型) var count = Number(inputElement.value) // 计算 count += 1 // 将值更新到页面 inputElement.value = count // 操作与之关联的减按钮 var minuButton = inputElement.previousSibling.previousSibling if (count > 1) { minuButton.removeAttribute('disabled') } else { minuButton.setAttribute('disabled', 'disabled') } // 更新与之关联的小计 // 获取单价 var btnTdElement = targetButton.parentNode var priTdElement = btnTdElement.previousSibling.previousSibling
var priceElement = priTdElement.childNodes[1] var price = parseFloat(priceElement.firstChild.nodeValue) var subtotal = price *= count if (String(subtotal).indexOf('.') === -1) { subtotal = subtotal + '.00' } var subTdElement = btnTdElement.nextSibling.nextSibling var subtotalElement = subTdElement.childNodes[1] subtotalElement.textContent = subtotal // console.log(price)
// 复选框 var trElement = btnTdElement.parentNode var checkTdElement = trElement.childNodes[1] var checkboxElement = checkTdElement.firstChild checkboxElement.setAttribute('checked', 'checked')
// 将当前这一行的背景颜色修改 #fff4e8 trElement.style.backgroundColor = '#fff4e8' } } for (var i = 0; i < minusButtons.length; i++) { var minusButton = minusButtons[i] minusButton.onclick = function (event) { var targetButton = event.target // 操作与之关联的input元素 var inputElement = targetButton.nextSibling.nextSibling console.log(inputElement) // 获取数量(string类型) var count = Number(inputElement.value) // 计算 count -= 1 // 将值更新到页面 inputElement.value = count
// 计算小计 var btnTdElement = targetButton.parentNode var priTdElement = btnTdElement.previousSibling.previousSibling
var priceElement = priTdElement.childNodes[1] var price = parseFloat(priceElement.firstChild.nodeValue) var subtotal = price *= count if (String(subtotal).indexOf('.') === -1) { subtotal = subtotal + '.00' } var subTdElement = btnTdElement.nextSibling.nextSibling var subtotalElement = subTdElement.childNodes[1] subtotalElement.textContent = subtotal // console.log(price)
// 操作减按钮 if (count <= 1) { targetButton.setAttribute('disabled', 'disabled') // 复选框 var trElement = btnTdElement.parentNode var checkTdElement = trElement.childNodes[1] var checkboxElement = checkTdElement.firstChild checkboxElement.removeAttribute('checked') // 将当前这一行的背景颜色修改 #fff4e8 trElement.style.backgroundColor = 'white' }
} } // 删除按钮 var delButtons = document.getElementsByName("delete") for (var i = 0; i < delButtons.length; i++) { var delButton = delButtons[i] delButton.onclick = function (event) { var targetButton = event.target
var currentTrElement = targetButton.parentNode.parentNode var parentElement = currentTrElement.parentNode parentElement.removeChild(currentTrElement)
var numberElement = document.getElementById('number') var number = parseInt(numberElement.textContent) number -= 1 numberElement.textContent = number /* 阻止a标签跳转 1. 通过调用调用 preventDefault() 2. 时间的处理函数 return 语句 -> returnfalse */ event.preventDefault() } }
// 全选 var checkall = document.getElementById("checkall") checkall.onclick = function () { console.log(1) console.log(checkall.checked) var checkElements = document.getElementsByName("check") var cart = document.getElementById('cart') var trElements = cart.getElementsByTagName('tr') if (checkall.checked) { for (var i = 0; i < checkElements.length; i++) { var checkElement = checkElements[i] checkElement.setAttribute("checked", "checked") } for (var i = 1; i < trElements.length; i++) { var trElement = trElements[i] trElement.style.backgroundColor = '#fff4e8' }
} else { for (var i = 0; i < checkElements.length; i++) { var checkElement = checkElements[i] checkElement.removeAttribute('checked') } for (var i = 1; i < trElements.length; i++) { var trElement = trElements[i] trElement.style.backgroundColor = 'transparent' } }