全选反选案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
table {
width: 500px;
border: 1px solid #ccc;
border-collapse: collapse;
margin: 100px auto;
}
th,
td {
width: 80px;
height: 40px;
border: 1px solid #ccc;
text-align: center;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th><input type="checkbox" class="checkAll">全选</th>
<th>商品</th>
<th>商家</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="check"></td>
<td>魅族20</td>
<td>魅族</td>
<td>2999</td>
</tr>
<tr>
<td><input type="checkbox" class="check"></td>
<td>魅族20pro</td>
<td>魅族</td>
<td>4399</td>
</tr>
<tr>
<td><input type="checkbox" class="check"></td>
<td>魅族20 infinity</td>
<td>魅族</td>
<td>7399</td>
</tr>
<tr>
<td><input type="checkbox" class="check"></td>
<td>魅族18</td>
<td>魅族</td>
<td>2999</td>
</tr>
</tbody>
</table>
<script>
const checkAll = document.querySelector('.checkAll')
const checks = document.querySelectorAll('.check')
checkAll.addEventListener('click', function () {
checks.forEach(ele => ele.checked = this.checked)
})
checks.forEach(ele => {
ele.addEventListener('change', function () {
checkAll.checked = Array.from(checks).every(ele => ele.checked === true)
})
})
</script>
</body>
</html>
购物车案例
第一种方式:forEach遍历数组加到str中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
table {
width: 800px;
border: 1px solid #ccc;
border-collapse: collapse;
margin: 100px auto 0;
}
th,
td {
width: 120px;
height: 60px;
text-align: center;
border: 1px solid #ccc;
}
li {
list-style: none;
font-size: 12px;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>产品</th>
<th>规格</th>
<th>价格</th>
<th>数量</th>
<th>总价</th>
</tr>
</thead>
<tbody>
<!-- <tr>
<td>小米手机</td>
<td>白色</td>
<td>2899</td>
<td>x23</td>
<td>66677</td>
</tr> -->
</tbody>
<tr class="total">
<td colspan="5"></td>
</tr>
</table>
<script>
const goodsList = [
{
name: '小米空调',
spec: {
color: '白色',
size: '80cm * 30cm'
},
price: 3499,
count: 10,
},
{
name: '魅族20',
spec: {
color: '白色',
storage: '12+512'
},
price: 2999,
count: 10,
gift: '手机壳,耳机'
},
{
name: '魅族20pro',
spec: {
color: '粉色',
storage: '12+512'
},
price: 3499,
count: 10,
gift: '手机壳,耳机'
},
{
name: '魅族20 infinity',
spec: {
color: '白色',
storage: '12+512'
},
price: 7399,
count: 10,
gift: '手机壳,20周年限定纪念品'
}
]
let str = ''
const tbody = document.querySelector('tbody')
goodsList.forEach(ele => {
const { name, spec, price, count, gift } = ele
const { color, size, storage } = spec
let text = Object.values(spec).join(' / ')
let giftStr = ''
gift ? gift.split(',').forEach(ele => {
giftStr += `【赠品】${ele}`
}) : '';
str += `
${name} ${giftStr}
|
${text} |
${price} |
x${count} |
${price * count} |
`
tbody.innerHTML = str
})
const total = goodsList.reduce((prev, current) => prev + current.price * current.count, 0)
const sum = document.querySelector('.total td')
sum.innerHTML = `合计:${total.toFixed(2)}`
</script>
</body>
</html>
第二种方式:map+join组合转换成字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table {
width: 600px;
margin: 100px auto;
border-collapse: collapse;
border: 1px solid #f00;
}
th,
td {
border: 1px solid #f00;
height: 40px;
line-height: 40px;
text-align: center;
}
span {
color: red;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>产品 </th>
<th>规格 </th>
<th>价格 </th>
<th>数量 </th>
<th>总价 </th>
</tr>
</thead>
<tbody>
</tbody>
<tr align="right">
<td colspan="5">合计:<span class="amount">1526</span></td>
</tr>
</table>
<script>
const goodsList = [
{
id: "4001172",
name: "小米手机",
price: 2899,
spec: { color: '白色' },
count: 23,
gift: "手机壳,手机膜"
},
{
id: "4001173",
name: "小米1",
price: 2199,
spec: { color: '白色' },
count: 23,
gift: "手机壳,手机膜"
},
{
id: "4001174",
name: "小米摄像头",
price: 3700,
spec: { color: '黑色' },
count: 23
},
{
id: "4001175",
name: "小米空调",
price: 3100,
spec: { color: '白色', size: "40cm*40cm" },
count: 23,
gift: "壳,防尘布"
}
]
document.querySelector('tbody').innerHTML = goodsList.map(ele => {
const { name, price, count, spec, gift } = ele
console.log(gift)
const text = Object.values(spec).join('/')
const str = gift ? gift.split(',').map(ele => `【赠品】${ele}`).join('') : ""
return `
${name}
${str}
|
${text}
|
${price.toFixed(2)} |
x${count} |
${(count * price).toFixed(2)} |
`;
}).join('')
const total = goodsList.reduce((prev, current) => prev + current.price * current.count, 0)
console.log(total.toFixed(2))
document.querySelector('.amount').innerHTML = total.toFixed(2)
</script>
</body>
</html>