
批改狀態(tài):合格
老師批語:
every(callback): 對數(shù)組中每個(gè)成員進(jìn)行回調(diào)處理,只有全部為true,最終才返回true,否則就是false
類似” and “,” 邏輯與 “
例如:[5, 6, 7, 8].every(item => item > 3)
當(dāng)前數(shù)組中的元素全部都大于3返回true
在數(shù)組中只要存在大于6的成員,就是返回true[5, 6, 7, 8].some(item => item > 6)
什么是模塊?
模塊就是一個(gè)js代碼塊
封裝成模塊的js文件,內(nèi)部成員對外不可見,除非導(dǎo)出來,模塊解決了js的模塊化開發(fā)與代碼封裝問題
模塊解決了什么問題?
可維護(hù)性:每個(gè)模塊是獨(dú)立的,各寫各個(gè)互不影響,出錯(cuò)直接定位責(zé)任人
可復(fù)制性:只需要一條import指令就可以導(dǎo)入
避免污染全局空間:模塊處在自己的命名空間
模塊是一個(gè)js文件,顯然不能像之前一樣,將js代碼寫到html中
模塊要到一個(gè)獨(dú)立的js文件中,并使用一些特別的語法和關(guān)鍵字
<script type="module">
//聲明為模塊
//導(dǎo)入 userName變量 從 module1.js文件中
import {userName,hello} from './module1.js';
console.log(userName);
console.log(hello(userName));
//禁止重復(fù)聲明模塊成員
let userName; //報(bào)錯(cuò)
//模塊成員不允許更新
userName="小李";//報(bào)錯(cuò)
//別名導(dǎo)出解決以上報(bào)錯(cuò)
import {myName,echo} from './module1.js';
//別名導(dǎo)入也可解決以上報(bào)錯(cuò)
import {userName as firstName} from './module1.js';
//導(dǎo)入默認(rèn)模塊 不用{}
import Name from './module1.js';
//導(dǎo)入默認(rèn)成員和普通成員
import world,{email} from './module1.js';
//-------------------命名空間
// 命名空間是一個(gè)容器,內(nèi)部可以包括任何類型的數(shù)據(jù)
// 命名空間是一個(gè)對象,可以掛載到當(dāng)前全局中
//導(dǎo)入的所有變量掛載到 namespace 下
import * as namespace from "./module1.js";
</script>
//module1.js 文件
//export導(dǎo)出
export let userName="小明";
export function hello(name){
return "hello" +name;
}
//統(tǒng)一導(dǎo)出 在userName就不用再聲明exprot了
export{userName,hello}
//解決在腳本中不能聲明問題使用別名導(dǎo)出
export{userName as myName,hello as echo};
//默認(rèn)的成員
//一個(gè)模塊只允許一個(gè)默認(rèn)導(dǎo)出
let Name="小紅";
export default Name;
//導(dǎo)出列表中,既有默認(rèn)成員,也有普通成員
function world(name){
return name;
}
let email="admin@php.cn";
//world 別名default 則聲明成了默認(rèn)成員
export{ email,world as default};
點(diǎn)擊全選/全不選
原生:
const checkAll=document.querySelector('#check-all');
const items=document.getElementsByName('item');
checkAll.onchange=ev=>{
items.forEach(item=>item.checked=ev.target.checked);
}
//當(dāng)item都選上則全選按鈕選中
items.forEach(item=>(item.onchange=()=>(checkAll.checked=[...items].every(item=>item.checked)))
);
jquery實(shí)現(xiàn)
$("#check-all").change(function(){
console.log( $(":checkbox[name='item']"));
$(":checkbox[name='item']").prop('checked',this.checked)
});
$(":checkbox[name='item']").change(ev=>$("#check-all").prop("checked",ev.target.checked));
計(jì)算總價(jià):
感覺方法欠妥,但又想不出更好的了。
實(shí)現(xiàn)改變狀態(tài),自動(dòng)計(jì)算金額
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>購物車</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<table>
<caption>
購物車
</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 class="inputt" type="number" min="1" value="1" /></td>
<td class="amount">xxxx</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 class="inputt" type="number" min="1" value="2" /></td>
<td class="amount">xxxx</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 class="inputt" type="number" min="1" value="1" /></td>
<td class="amount">xxxx</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 class="inputt" type="number" min="1" value="2" /></td>
<td class="amount">xxxx</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 class="inputt" type="number" min="1" value="1" /></td>
<td class="amount">xxxx</td>
</tr>
</tbody>
<tfoot>
<tr style="font-weight: bolder; font-size: 1.2em">
<td colspan="5">總計(jì):</td>
<td id="sum">xxxx</td>
<td id="total-amount">xxxx</td>
</tr>
</tfoot>
</table>
<div style="width: 90%; margin: 10px auto">
<button style="float: right; width: 100px">結(jié)算</button>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js">
</script>
<script>
//----jquery---------------------
// $("#check-all").change(function(){
// console.log( $(":checkbox[name='item']"));
// $(":checkbox[name='item']").prop('checked',this.checked)
// });
// $(":checkbox[name='item']").change(ev=>$("#check-all").prop("checked",ev.target.checked));
//點(diǎn)擊全選則都選上
const checkAll=document.querySelector('#check-all');
const items=document.getElementsByName('item');
checkAll.onchange=ev=>{
items.forEach(item=>item.checked=ev.target.checked);
}
//------------------------計(jì)算總價(jià)----------------------------------------
//---------------------根據(jù)復(fù)選框狀態(tài),自動(dòng)計(jì)算金額-----------------------------------------------
// 得到所有臺(tái)數(shù)標(biāo)簽
const numInputs = document.querySelectorAll('tbody input[type="number"]');
//單價(jià)標(biāo)簽
const prices=document.querySelectorAll("tbody .price");
//金額標(biāo)簽
const amount=document.querySelectorAll(".amount");
items.forEach(item=>(item.onchange=()=>{
//改變?nèi)x按鈕狀態(tài)
checkAll.checked=[...items].every(item=>item.checked);
//將復(fù)選框狀態(tài)放到一個(gè)數(shù)組里
let numite=[...items].map(item=>item.checked);
for(let i=0;i<numite.length;i++){
if(!numite[i]){
//根據(jù)選擇狀態(tài)false 改變 臺(tái)數(shù)和單價(jià)的 class
numInputs[i].classList.replace('inputt','inputf');
prices[i].classList.replace('price','pricef');
amount[i].classList.replace('amount','amountf');
}else{
numInputs[i].classList.replace('inputf','inputt');
prices[i].classList.replace('pricef','price');
amount[i].classList.replace('amountf','amount');
}
}
}));
//--------------------弊端:未選中的商品,改變其數(shù)量則無法計(jì)算出其總金額------------------------------------------
//-------------------------全選按鈕沒有實(shí)現(xiàn),全選了計(jì)算總金額-----------------------------------------------
//------------------------自己實(shí)現(xiàn)方式雖然解決了問題了,但總感覺存在弊端,不是完美的解決方式---------------------
const numInput = document.querySelectorAll('tbody input.inputt[type="number"]');
numInput.forEach(input => (onchange = autoCalculate));
// 用戶更新數(shù)量時(shí)觸發(fā)自動(dòng)計(jì)算
window.onload=autoCalculate;
// 封裝一個(gè)自動(dòng)計(jì)算的函數(shù)
function autoCalculate() {
//計(jì)算商品數(shù)量
const numbers=document.querySelectorAll('tbody input.inputt[type="number"]');
const numArr=[...numbers].map(item=>item.value*1);
//計(jì)算選擇了的臺(tái)數(shù)
let sum=numArr.reduce((pre,cur)=>pre+cur);
//計(jì)算總金額 數(shù)量* 單價(jià)
const prices=document.querySelectorAll("tbody .price");
const priceArr=[...prices].map(item=>item.textContent*1);
const amountArr =[priceArr,numArr].reduce((total, curr)=>total.map((item,index)=>item*curr[index]));
// 計(jì)算總金額
//只計(jì)算選上的數(shù)據(jù)
let totalAmount = amountArr.reduce((pre, cur) => pre + cur);
//將結(jié)果渲染到頁面中
document.querySelector('#sum').textContent=sum;
document.querySelector('#total-amount').textContent=totalAmount;
// 每個(gè)商品的金額
document.querySelectorAll(".amount").forEach((item, index) => (item.textContent = amountArr[index]));
}
</script>
</body>
</html>
微信掃碼
關(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)