
批改狀態(tài):合格
老師批語(yǔ):非常出色
table {
border-collapse: collapse;
width: 90%;
text-align: center;
margin: auto;
}
table caption {
margin-bottom: 15px;
font-size: 1.5rem;
}
table th,
table td {
border-bottom: 1px solid #ccc;
padding: 5px;
font-weight: normal;
}
table thead tr:first-of-type {
background-color: #e6e6e6;
height: 3em;
}
table input[type="checkbox"] {
width: 1.5em;
height: 1.5em;
}
table tbody tr {
border-bottom: 1px solid #ccc;
}
table tbody tr:hover {
background-color: #f6f6f6;
cursor: pointer;
}
tbody img {
width: 3em;
}
tbody input[type="number"] {
width: 3em;
}
button {
width: 150px;
height: 30px;
outline: none;
border: none;
background-color: teal;
color: white;
letter-spacing: 5px;
}
button:hover {
opacity: 0.7;
cursor: pointer;
}
<table>
<caption>
購(gòu)物車
</caption>
<thead>
<tr>
<!-- 全選復(fù)選框 -->
<th><input type="checkbox" name="checkAll" id="check-all" checked /><label for="check-all">全選</label></th>
<th>圖片</th>
<th>品名</th>
<th>單位</th>
<th>單價(jià)/元</th>
<th>數(shù)量</th>
<th>金額/元</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="item" value="SN-1020" checked />
</td>
<td>
<a href=""><img src="images/p1.jpg" alt="" /></a>
</td>
<td>iPhone 11</td>
<td>臺(tái)</td>
<td class="price">4799</td>
<td><input type="number" min="1" value="1" /></td>
<td class="amount">4799</td>
</tr>
<tr>
<td>
<input type="checkbox" name="item" value="SN-1020" checked />
</td>
<td>
<a href=""><img src="images/p2.jpg" alt="" /></a>
</td>
<td>小米pro 11</td>
<td>部</td>
<td class="price">3999</td>
<td><input type="number" min="1" value="2" /></td>
<td class="amount">7998</td>
</tr>
<tr>
<td>
<input type="checkbox" name="item" value="SN-1030" checked />
</td>
<td>
<a href=""><img src="images/p3.jpg" alt="" /></a>
</td>
<td>MacBook Pro</td>
<td>臺(tái)</td>
<td class="price">18999</td>
<td><input type="number" min="1" value="1" /></td>
<td class="amount">18999</td>
</tr>
<tr>
<td>
<input type="checkbox" name="item" value="SN-1040" checked />
</td>
<td>
<a href=""><img src="images/p4.jpg" alt="" /></a>
</td>
<td>小米75電視</td>
<td>臺(tái)</td>
<td class="price">5999</td>
<td><input type="number" min="1" value="2" /></td>
<td class="amount">11998</td>
</tr>
<tr>
<td>
<input type="checkbox" name="item" value="SN-1050" checked />
</td>
<td>
<a href=""><img src="images/p5.jpg" alt="" /></a>
</td>
<td>Canon 90D單反</td>
<td>臺(tái)</td>
<td class="price">9699</td>
<td><input type="number" min="1" value="1" /></td>
<td class="amount">9699</td>
</tr>
</tbody>
<tfoot>
<tr style="font-weight: bolder; font-size: 1.2em">
<td colspan="5">總計(jì):</td>
<td id="sum">7</td>
<td id="total-amount">53493</td>
</tr>
</tfoot>
</table>
<div style="width: 90%; margin: 10px auto">
<button style="float: right; width: 100px">結(jié)算</button>
</div>
// 1.獲取全選按鈕,每個(gè)獨(dú)立的商品復(fù)選框
const checkAll = document.querySelector('#check-all');
const checkItems = document.getElementsByName("item");
// 2. 將當(dāng)前的全選的狀態(tài)變化賦值給每個(gè)商品復(fù)選框
checkAll.addEventListener('change', (ev) => {
checkItems.forEach(item => {
item.checked = ev.target.checked
});
// 根據(jù)選中狀態(tài)計(jì)算商品的總金額和總數(shù)量
amountTotal();
});
// 3. 為每個(gè)單獨(dú)商品的復(fù)選框添加事件
checkItems.forEach(item => {
item.addEventListener('change', () => {
checkAll.checked = [...checkItems].every(item => item.checked);
// 根據(jù)選中狀態(tài)計(jì)算商品的總金額和總數(shù)量
amountTotal();
})
});
// 4.為數(shù)量按鈕添加事件,動(dòng)態(tài)計(jì)算商品總量和金額
const numInput = document.querySelectorAll('tbody input[type="number"]');
numInput.forEach(input => input.addEventListener('change', autoCalculate));
// 頁(yè)面加載完成自動(dòng)計(jì)算
window.addEventListener('load', autoCalculate);
// 5. 自動(dòng)計(jì)算方法
function autoCalculate() {
// 獲取所有商品單價(jià)節(jié)點(diǎn)
const prices = document.querySelectorAll('tbody .price');
// 獲得所有商品單價(jià)金額
const priceArr = [...prices].map(price => +price.innerText);
console.log(priceArr);
// 獲取所有數(shù)量按鈕節(jié)點(diǎn)
const numbers = document.querySelectorAll('tbody input[type="number"]');
// 獲得所有商品數(shù)量數(shù)組
const numArr = [...numbers].map(num => +num.value);
console.log(numArr);
// 計(jì)算單個(gè)商品金額 = 數(shù)量*單價(jià)
let amountArr = [priceArr, numArr].reduce((total, curr) => {
return total.map((item, index) => {
return item * curr[index]
})
});
// 將每一個(gè)商品金額渲染到頁(yè)面中
document.querySelectorAll('.amount').forEach((item, index) => {
item.innerText = amountArr[index]
})
// 計(jì)算總數(shù)量總數(shù)量
amountTotal();
}
// 根據(jù)復(fù)選框選中狀態(tài)計(jì)算總金額和總數(shù)量
function amountTotal() {
// 獲取所有數(shù)量按鈕節(jié)點(diǎn)
const numbers = document.querySelectorAll('tbody input[type="number"]');
// 獲得所有商品數(shù)量數(shù)組
const numArr = [...numbers].map(num => {
// 獲取當(dāng)前數(shù)量節(jié)點(diǎn)父節(jié)點(diǎn)下的check復(fù)選框
const check = num.parentElement.parentElement.querySelector('input[type="checkbox"]');
// 過濾出選中的商品數(shù)量數(shù)組
return check.checked ? +num.value : 0;
});
console.log(numArr);
// 獲取所有單個(gè)商品總金額節(jié)點(diǎn)
const amounts = document.querySelectorAll('.amount');
// 獲得所有單個(gè)商品總金額數(shù)組
const amountArr = [...amounts].map(amount => {
// 獲取當(dāng)前數(shù)量節(jié)點(diǎn)父節(jié)點(diǎn)下的check復(fù)選框
const check = amount.parentElement.querySelector('input[type="checkbox"]');
// 過濾出選中的商品數(shù)量數(shù)組
return check.checked ? +amount.innerText : 0;
});
console.log(amountArr);
// 計(jì)算總數(shù)量
document.querySelector('#sum').innerText = `${numArr.reduce((pre, cur) => pre + cur)}件`;
// 計(jì)算總金額
document.querySelector('#total-amount').textContent = `¥${amountArr.reduce((pre, curr) => pre + curr)}`;
}
實(shí)現(xiàn)效果:
預(yù)覽地址:http://easys.ltd/shopcart/
一個(gè)模塊就是一個(gè)獨(dú)立的js代碼塊文件,下面將介紹module中的export和import概念。
ES6 module 模塊功能使用 export 導(dǎo)出模塊的內(nèi)容,并使用 import 導(dǎo)入模塊的內(nèi)容。
示例:
// math.js(定義模塊)
exports.add = function(a, b) {
return a + b;
};
// app.js(使用模塊)
var math = require('./math');
var rs = math.add(1, 2);
console.log(rs);
創(chuàng)建ES6模塊時(shí),可使用export關(guān)鍵字導(dǎo)出(對(duì)外提供)模塊的內(nèi)容,如函數(shù)、對(duì)象以及原始變量等等。
export 導(dǎo)出方案有2種:Named exports(命名導(dǎo)出;每個(gè)模塊可有多個(gè))和 Default exports(默認(rèn)導(dǎo)出;每個(gè)模塊只能一個(gè))。
說明:使用 export + 名稱 的形式導(dǎo)出模塊的內(nèi)容。
注意:在 import 導(dǎo)入過程中,需指定這些名稱。
// 1) 聲明時(shí)導(dǎo)出
export let myVar = 'a';
export const MY_CONST = 'c';
export const getName = () => {}...
export const getAge = () => {}...
export function getName(){
return ...
}
// 2) 聲明后導(dǎo)出
var myVar = 'a';
export const MY_CONST = 'c';
const getName = () =>{} ...
const getAge = () =>{} ...
export {myVar,MY_CONST,getName,getAge}
// 3) 別名導(dǎo)出
var myVar3 = 'a';
export { myVar3 as myVar };
// math.js
export function add(a, b) {
return a + b;
}
// app.js:導(dǎo)入含有命名導(dǎo)出的模塊時(shí),需要指定成員名稱
import { add } from './math.js';
console.log(add(1, 2)); // => 3
說明:使用 export default 導(dǎo)出模塊默認(rèn)的內(nèi)容,每個(gè)模塊只能有一個(gè) export default。
// 1) 聲明時(shí)導(dǎo)出 匿名函數(shù)
export default expression;
export default function() {
return ...
}
export default () => {}...
// 2) 別名設(shè)置為default導(dǎo)出
export default function name1() {}
export { name1 as default };
默認(rèn)導(dǎo)出聲明的是一個(gè)表達(dá)式,通常沒有名字,導(dǎo)入時(shí)需指定模塊名稱。
// math.js
export function add(a, b) {
return a + b;
}
export default function cube(x) {
return x * x * x;
}
// app.js:導(dǎo)入默認(rèn)導(dǎo)出的模塊時(shí),需要指定模塊名稱
import cube from './math.js';
console.log(cube(3)); // => 27
// 若想同時(shí)導(dǎo)入含有默認(rèn)導(dǎo)出、命名導(dǎo)出的模塊,只需要導(dǎo)入時(shí)用','隔開
import cube, { add } from './math.js';
// 1)導(dǎo)入模塊的默認(rèn)導(dǎo)出內(nèi)容
import defaultExport from 'module-name';
// 2)導(dǎo)入模塊的命名導(dǎo)出內(nèi)容
import { export1, export2 } from 'module-name';
import { export as alias } from 'module-name'; // 修改別名
import * as name from 'module-name'; // 導(dǎo)入模塊內(nèi)的所有命名導(dǎo)出內(nèi)容
// 3)導(dǎo)入模塊的默認(rèn)導(dǎo)出、命名導(dǎo)出
import defaultExport, { export1, export2 } from 'module-name';
import defaultExport, * as name from 'module-name';
說明:導(dǎo)入默認(rèn)導(dǎo)出的模塊時(shí),需要指定模塊名稱
// math.js
export default function cube(x) {
return x * x * x;
}
// app.js:導(dǎo)入默認(rèn)導(dǎo)出的模塊時(shí),需要指定模塊名稱
import cube from './math.js';
console.log(cube(3)); // => 27
import user from './default.js' // 如果使用這種引入請(qǐng)導(dǎo)出匿名函數(shù)
說明:導(dǎo)入模塊時(shí)可使用大括號(hào)包含指定命名成員;也可以用 * as moduleName 的形式把此模塊的所有命名導(dǎo)出作為某個(gè)對(duì)象的成員。
// math.js
export function add(a, b) {
return a + b;
}
// app.js:指定使用math模塊的add命名導(dǎo)出
import { add } from './math.js';
console.log(add(1, 2)); // => 3
// 按需引入
import { getName, getAge } from './utils.js'
// 導(dǎo)入所有的命名導(dǎo)出作為math對(duì)象的成員
import * as math from './math.js';
console.log(math.add(1, 2)); // => 3
// 將引入的模塊賦值到user對(duì)象中
import * as user from './utils.js'
// 結(jié)構(gòu)會(huì)變成如下樣式
// user = {
// getAge: function(){ ... }
// getName: function(){ ... }
// }
說明:僅導(dǎo)入模塊時(shí),只會(huì)執(zhí)行模塊的全局函數(shù),不會(huì)導(dǎo)入任何成員。
// math.js
export function add(a, b) {
return a + b;
}
function hello() {
console.log('hello math.js');
}
hello();
// app.js
import { add } from './math.js'; // => hello math.js
注意:模塊成員在當(dāng)前使用環(huán)境中,既不能重復(fù)聲明,也不能更新(相對(duì)應(yīng)常量)
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)