欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery利用DOM遍歷實(shí)現(xiàn)商城結(jié)算系統(tǒng)實(shí)戰(zhàn)

 更新時(shí)間:2022年06月29日 14:55:44   作者:張阿起  
這篇文章主要介紹了jQuery利用DOM遍歷實(shí)現(xiàn)商城結(jié)算系統(tǒng)實(shí)戰(zhàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具一定的參考價(jià)值,感興趣的小伙伴可以參考一下

效果圖

一.body

<body>
		<div style="background-color: antiquewhite;width: 500px;height: 450px;border: 1px solid aquamarine;border-radius: 20px;">
			<table>
				<thead align="center">
					<p style="text-align: center;"><span style="font-size: 30px;color:mediumslateblue;border: 2px solid red;border-radius: 10px;background-color:navajowhite;">結(jié)算中心</span></p>
					<tr class="tr1">
						<th style="position: absolute;"><input type="checkbox" id="cballS">全選</th>
						<th  style="position: absolute;left: 100px;">商品</th>
						<th style="position: absolute;left: 190px;">單價(jià)</th>
						<th style="position: absolute;left: 290px;">數(shù)量</th>
						<th style="position: absolute;left: 370px;"><input type="hidden" value="0" />小計(jì)</th>
						<th style="position: absolute;left: 450px;">操作</th>
					</tr>
					<!--//作為換行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>

				</thead>

				<tbody>
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">選中</td>
						<td style="position: absolute;left: 100px;">vivoX86</td>
						<td style="position: absolute;left: 190px;">1299.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;" />
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)" >刪除</a></td>
					</tr>
					<!--//作為換行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr class="tr1">
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">選中</td>
						<td style="position: absolute;left: 100px;">oppoS23</td>
						<td style="position: absolute;left: 190px;">1399.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">刪除</a></td>
					</tr>
					<!--//作為換行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">選中</td>
						<td style="position: absolute;left: 100px;">HUAWEIS7</td>
						<td style="position: absolute;left: 190px;">1499.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">刪除</a></td>
					</tr>
					<!--//作為換行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">選中</td>
						<td style="position: absolute;left: 100px;">Mi 8s</td>
						<td style="position: absolute;left: 190px;">1599.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">刪除</a></td>
					</tr>
					<!--//作為換行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">選中</td>
						<td style="position: absolute;left: 100px;">iPhone13</td>
						<td style="position: absolute;left: 190px;">1699.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">刪除</a></td>
					</tr>
				</tbody>
				<tfoot>
					<!--//作為換行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<th><span style="background-color:aqua;border:1px solid darkblue;border-radius: 5px;display:inline-block;width: 80px;">總金額</span>&nbsp;:&emsp;<span id="moneys" style="display:inline-block;width: 100px;background-color: seagreen;border-radius: 10px;">0</span>元</th>
						<th>&emsp;<a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >結(jié)算</a></th>
					</tr>
				</tfoot>
			</table>
		</div>
	</body>

二.css

<style>
			.cbAll{
				border: 1px solid darkblue;
				background-color: darkgoldenrod;
				border-radius: 10px;
				position:absolute;
				
			}
		</style>

三.JavaScript

<script type="text/javascript" src="js/jquery.min.js" ></script>
		<script>
			$(function(){
				$("[type=button]").click(function(){
					//獲取按鈕
					var btn=$(this).val();
					//獲取父元素再獲取父元素的上一個(gè)同胞元素
					var Fu=parseFloat($(this).parent().prev().html());
					//獲取父元素后獲取父元素下一個(gè)同胞元素
					//var FuX=parseFloat($(this).parent().next().html());
					if(btn=="+"){
						//獲取數(shù)量
						var preVa=parseInt($(this).prev().val());
						//給數(shù)量加1
						$(this).prev().val(preVa+1)
						//獲取并賦值數(shù)量
						preVa=parseInt($(this).prev().val());
						//給父元素下一個(gè)同胞元素賦值,并保留小數(shù)點(diǎn)后兩位
						var ptsum=(Fu*preVa).toString();
						var newSum="";
						for(i=0;i<ptsum.length;i++){
							if(ptsum[i]=="."){
								if(ptsum[i+1]!=undefined){
									newSum+=ptsum[i]+ptsum[i+1];
								}
								if(ptsum[i+2]!=undefined){
									newSum+=ptsum[i+2];
								}
								break;
							}
							newSum+=ptsum[i];
						};
						$(this).parent().next().html(newSum);
						Smon();
						
					}else{
						//獲取數(shù)量
						var preVa=parseInt($(this).next().val());
						if(preVa==0){
							return alert("不能再少了!");
						}
						//給數(shù)量減一
						$(this).next().val(preVa-1);
						//獲取并賦值元素
						preVa=parseInt($(this).next().val());
						//給父元素下一個(gè)同胞元素賦值,并保留小數(shù)點(diǎn)后兩位
						var ptsum=(preVa*Fu).toString();
						var newSum="";
						for(i=0;i<ptsum.length;i++){
							if(ptsum[i]=="."){
								if(ptsum[i+1]!=undefined){
									newSum+=ptsum[i]+ptsum[i+1];
								}
								if(ptsum[i+2]!=undefined){
									newSum+=ptsum[i+2];
								}
								break;
							}
							newSum+=ptsum[i];
						}
						$(this).parent().next().html(newSum);
						Smon();
						
					};
					
				});
				
				//全選按鈕
				$("#cballS").click(function(){
					
					var flag=$(this).prop("checked");
					$("[type=checkbox]").each(function(){
						$(this).prop("checked",flag);
						
					});
					Smon()
				});
				//輸入框發(fā)生變化刷新小計(jì)
				
				$("[type='text']").change(function(){
							
							//獲取元素內(nèi)容
							var ptNum=parseFloat($(this).val());
							
							if(ptNum<=0 || isNaN(ptNum)){
								$(this).val(0);
								return false;
							};
							var n1=ptNum.toString();
							for(i=0;i<n1.length;i++){
								//不能為小數(shù)或者不能大于100
								if(n1[i]=="." || i==2){
									$(this).val(0);
									return false;
								};
							};
							
							//獲取單價(jià)
							var ptDj=parseFloat($(this).parent().prev().html());
							//獲取總額
							var ptSum=(ptNum*ptDj).toString();
							var newSum="";
							//保留小數(shù)點(diǎn)后兩位
							for(i=0;i<ptSum.length;i++){
								if(ptSum[i]=="."){
									if(!isNaN(ptSum[i+1])){
										newSum+=ptSum[i]+ptSum[i+1];
										if(!isNaN(ptSum[i+2])){
											newSum+=ptSum[i+2];
										};
										break;
									};

								};
								newSum+=ptSum[i];
							}
							//設(shè)置小計(jì)
							$(this).parent().next().html(newSum);
							Smon();
				});
				
				
			});
			//計(jì)算總金額
			function Smon(){
					var sum=0;
					//循環(huán)遍歷所有選中按鈕后邊的值
					$("[type=checkbox]:checked").each(function(){
						//獲取選中按鈕同級(jí)的小計(jì)
						var xiaoji=parseFloat($(this).parent().next().next().next().next().html());
						//判斷是否是第一行的,isNaN判斷是否可以轉(zhuǎn)數(shù)值
						if(isNaN(xiaoji)){
							xiaoji=0;
						}
						sum+=xiaoji;
					});
					sum=parseFloat(sum).toString();
					var newSum="";
					//總計(jì)金額默認(rèn)保存小數(shù)點(diǎn)后兩位,循環(huán)拼接并賦值給新的變量
					for(i=0;i<sum.length;i++){
						if(i<=7){
							if(sum[i]=="."){
								if(sum[i+1]!=undefined){
									newSum+=sum[i]+sum[i+1];
								}
								if(sum[i+2]!=undefined){
									newSum+=sum[i+2];
								}
								break;
							}
							newSum+=sum[i];
						}
					}
					//賦值
					$("#moneys").html(newSum);
			};
			//選中按鈕判斷
			function chClick(){
					//獲取全選按鈕
					var len=$("#cballS").prop("checked");
					//獲取所有可選擇的按鈕為true的長(zhǎng)度
					var len2=$("[type=checkbox]:checked").length;
					//獲取所有可選中按鈕的長(zhǎng)度
					var len3=$("[type=checkbox]").length;
					//由于函數(shù)執(zhí)行是按照自上而下,調(diào)用一次執(zhí)行一次的方式,所以當(dāng)?shù)谝淮闻袛噙x中按鈕為5個(gè)時(shí),就去勾選全選按鈕,
					//當(dāng)以后每次判斷再進(jìn)來時(shí),只會(huì)有兩種結(jié)果,一個(gè)全選是true,一個(gè)全選是false,只需要判斷全選是什么狀態(tài)就將狀態(tài)改變另一個(gè)即可。
					if(len2==len3-1 & len==false){
						$("#cballS").prop("checked",true);
					}else if(len2>=len3-1 & len==true){
						$("#cballS").prop("checked",false);
					};
					//執(zhí)行完調(diào)用總計(jì)函數(shù)
					Smon();
			};
			
			//刪除,使用函數(shù)調(diào)用完成
			function del(th){
				var aa=$(th).parent().parent().next().remove();
				$(th).parent().parent().remove();
				Smon();
			};
		
		</script>

四.小結(jié)

本章使用所學(xué)jQueryDOM遍歷實(shí)現(xiàn)了一個(gè)實(shí)用簡(jiǎn)易小商城結(jié)算系統(tǒng),實(shí)現(xiàn)方式不只這一種,各位感興趣的小伙伴,可以自行探索.

到此這篇關(guān)于jQuery利用DOM遍歷實(shí)現(xiàn)商城結(jié)算系統(tǒng)實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)jQuery DOM遍歷 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論