
批改狀態(tài):合格
老師批語(yǔ):jQuery中, 要注意當(dāng)前是jQ對(duì)象,還是js對(duì)象, 這個(gè)很重要
使用jQuery實(shí)現(xiàn)功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>換膚</title>
<style>
.container {
width: 100px;
display: flex;
box-sizing: border-box;
}
.container>img {
width: 100%;
margin: 0 3px 0 0;
border: 3px solid white;
opacity: 0.6;
}
.container>img:hover {
opacity: 1;
cursor: pointer;
width: 110%;
}
html,
body {
height: 100%;
}
body {
background-image: url("imgs/03.jpg");
background-repeat: no-repeat;
background-size: 100% 100%;
}
</style>
</head>
<body>
<div class="container">
<img src="imgs/03.jpg" alt="">
<img src="imgs/06.jpg" alt="">
<img src="imgs/07.jpg" alt="">
</div>
</body>
<script src="../lib/jquery-3.5.1.js"></script>
<script>
$("div").click(function (ev) {
$("body").css("background-image", "url(" + ev.target.src + ")");
})
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自動(dòng)計(jì)算價(jià)格</title>
<style>
table {
border: 1px solid;
border-collapse: collapse;
margin: 20px auto;
width: 500px;
text-align: center;
}
tr td {
border: 1px solid;
}
caption {
margin-bottom: 10px;
}
div {
width: 500px;
margin: auto;
}
thead {
background-color: aquamarine;
}
button {
float: right;
width: 80px;
}
.tain {
box-sizing: border-box;
width: 50px;
text-align: center;
}
</style>
</head>
<body>
<table>
<caption>購(gòu)物車</caption>
<thead>
<tr>
<td><input type="checkbox" name="itemAll" id="itemAll" checked>全選</td>
<td>ID</td>
<td>品名</td>
<td>單位</td>
<td>單價(jià)/元</td>
<td>數(shù)量</td>
<td>金額/元</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="itemID" id="itemID" value="SN-1010" checked></td>
<td>SN-1010</td>
<td>MacBook Pro電腦</td>
<td>臺(tái)</td>
<td>18999</td>
<td>
<input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
</td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" name="itemID" id="itemID" value="SN-1020" checked></td>
<td>SN-1020</td>
<td>iPhone手機(jī)</td>
<td>部</td>
<td>4999</td>
<td>
<input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
</td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" name="itemID" id="itemID" value="SN-1030" checked></td>
<td>SN-1030</td>
<td>智能Ai音箱</td>
<td>只</td>
<td>399</td>
<td>
<input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
</td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" name="itemID" id="itemID" value="SN-1040" checked></td>
<td>SN-1040</td>
<td>SSD移動(dòng)硬盤(pán)</td>
<td>個(gè)</td>
<td>888</td>
<td>
<input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
</td>
<td></td>
</tr>
<tr>
<td><input type="checkbox" name="itemID" id="itemID" value="SN-1050" checked></td>
<td>SN-1050</td>
<td>黃山毛峰</td>
<td>斤</td>
<td>999</td>
<td>
<input type="number" class="tain" name="counter" value="1" min="0" step="1" id="">
</td>
<td></td>
</tr>
<tr>
<td colspan="5">總計(jì)</td>
<td id="totalNum"></td>
<td id="totalPrice"></td>
</tr>
</tbody>
</table>
<div>
<button>結(jié)算</button>
</div>
<script src="../lib/jquery-3.5.1.js"></script>
<script>
$("#itemAll").on("change", chend);
function chend(ev) {
$("input[name='itemID']").prop("checked", ev.target.checked);
if ((ev.target.checked)) {
$("input[type='number']").prop("value", 1);
} else {
$("input[type='number']").prop("value", 0);
}
// 獲取商品數(shù)量
Counts = getCounts(numbers);
// 獲取商品單價(jià)
Prices = getPrices(Price);
// 獲取商品金額
Amouts = getAmouts(Counts, Prices);
// 獲取商品總數(shù)量
totalNum = num(Counts);
// 獲取商品總金額
totalPrice = toPrice(Amouts);
// 渲染商品金額到頁(yè)面中
$("tbody>tr>td:last-of-type").each(function (key, value) {
$(value).html(Amouts[key]);
});
// 渲染商品總數(shù)量到頁(yè)面中
$("#totalNum").html(totalNum);
// 渲染商品總金額到頁(yè)面中
$("#totalPrice").html(totalPrice);
}
$("input[name='itemID']").change(function (ev) {
var count = 0;
// 循環(huán)按鈕選中狀態(tài)的個(gè)數(shù)
$("input[name='itemID']").each(function (key, value) {
if (value.checked) count++;
});
if (ev.target.checked) {
$(ev.target).parent().siblings().eq(4).children().prop("value", 1);
} else {
$(ev.target).parent().siblings().eq(4).children().prop("value", 0);
}
// 根據(jù)個(gè)數(shù)判斷全選按鈕是否要選ziyu
$("#itemAll").prop("checked", (count === $("input[name='itemID']").length));
// 獲取商品數(shù)量
Counts = getCounts(numbers);
// 獲取商品單價(jià)
Prices = getPrices(Price);
// 獲取商品金額
Amouts = getAmouts(Counts, Prices);
// 獲取商品總數(shù)量
totalNum = num(Counts);
// 獲取商品總金額
totalPrice = toPrice(Amouts);
// 渲染商品金額到頁(yè)面中
$("tbody>tr>td:last-of-type").each(function (key, value) {
$(value).html(Amouts[key]);
});
// 渲染商品總數(shù)量到頁(yè)面中
$("#totalNum").html(totalNum);
// 渲染商品總金額到頁(yè)面中
$("#totalPrice").html(totalPrice);
});
// 商品數(shù)量
var Counts = [];
var numbers = $("input[type='number']");
Counts = getCounts(numbers);
function getCounts(numbers) {
return numbers.map(function (key, item) {
return parseInt(numbers.get(key).value);
}).get();
};
console.log(Counts);
// 商品單價(jià)
var Prices = [];
var Price = $("tbody>tr>td:nth-of-type(5)");
Prices = getPrices(Price);
function getPrices(Price) {
return Price.map(function (key, value) {
return parseInt($(value).html());
}).get();
};
console.log(Prices);
// 計(jì)算商品金額
var Amouts = [];
Amouts = getAmouts(Counts, Prices);
function getAmouts(Counts, Prices) {
var amount = [];
for (var i = 0; i < Counts.length; i++) {
am = Counts[i] * Prices[i];
amount.push(am);
};
return amount;
}
console.log(Amouts);
// 把數(shù)據(jù)渲染到頁(yè)面中
$("tbody>tr>td:last-of-type").each(function (key, value) {
$(value).html(Amouts[key]);
});
// 獲取總數(shù)量
var totalNum = 0;
totalNum = num(Counts);
function num(Counts) {
return Counts.reduce(function (prev, next) {
return prev += next;
});
};
console.log(totalNum);
// 渲染到頁(yè)面中
$("#totalNum").html(totalNum);
// 獲取總金額
var totalPrice = 0;
totalPrice = toPrice(Amouts);
function toPrice(Amouts) {
return Amouts.reduce(function (prev, next) {
return prev += next;
});
};
console.log(totalPrice);
// 渲染到頁(yè)面中
$("#totalPrice").html(totalPrice);
// 綁定事件
numbers.on("change", autoCalculate);
function autoCalculate(ev) {
// 獲取商品數(shù)量
Counts = getCounts(numbers);
// 獲取商品單價(jià)
Prices = getPrices(Price);
// 獲取商品金額
Amouts = getAmouts(Counts, Prices);
// 獲取商品總數(shù)量
totalNum = num(Counts);
// 獲取商品總金額
totalPrice = toPrice(Amouts);
// 渲染商品金額到頁(yè)面中
$("tbody>tr>td:last-of-type").each(function (key, value) {
$(value).html(Amouts[key]);
});
// 渲染商品總數(shù)量到頁(yè)面中
$("#totalNum").html(totalNum);
// 渲染商品總金額到頁(yè)面中
$("#totalPrice").html(totalPrice);
}
</script>
</body>
</html>
1.對(duì)于換膚圖片案列,明白了一件事一開(kāi)始一直以為body的高度就是頁(yè)面的高度,一直想著為什么圖片的自適應(yīng)實(shí)現(xiàn)不了,查看控制器才發(fā)現(xiàn)body的高度沒(méi)設(shè)置的話是靠其子元素的高度撐開(kāi)的,不是頁(yè)面的高度。
2.自動(dòng)計(jì)算案列在獲取數(shù)量或者單價(jià)數(shù)組時(shí),用jQuery的map()方法得到的值用parseInt()方法后得到的是jQuery的對(duì)象而不是數(shù)組,用了get()方法后才是數(shù)組,我的想法是parseInt()轉(zhuǎn)為數(shù)值,那么應(yīng)該直接成數(shù)組不是嗎,這里有點(diǎn)不明白
微信掃碼
關(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)