最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

效果圖

一.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;">單價</th>
						<th style="position: absolute;left: 290px;">數(shù)量</th>
						<th style="position: absolute;left: 370px;"><input type="hidden" value="0" />小計</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();
					//獲取父元素再獲取父元素的上一個同胞元素
					var Fu=parseFloat($(this).parent().prev().html());
					//獲取父元素后獲取父元素下一個同胞元素
					//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());
						//給父元素下一個同胞元素賦值,并保留小數(shù)點后兩位
						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());
						//給父元素下一個同胞元素賦值,并保留小數(shù)點后兩位
						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ā)生變化刷新小計
				
				$("[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;
								};
							};
							
							//獲取單價
							var ptDj=parseFloat($(this).parent().prev().html());
							//獲取總額
							var ptSum=(ptNum*ptDj).toString();
							var newSum="";
							//保留小數(shù)點后兩位
							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è)置小計
							$(this).parent().next().html(newSum);
							Smon();
				});
				
				
			});
			//計算總金額
			function Smon(){
					var sum=0;
					//循環(huán)遍歷所有選中按鈕后邊的值
					$("[type=checkbox]:checked").each(function(){
						//獲取選中按鈕同級的小計
						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="";
					//總計金額默認(rèn)保存小數(shù)點后兩位,循環(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的長度
					var len2=$("[type=checkbox]:checked").length;
					//獲取所有可選中按鈕的長度
					var len3=$("[type=checkbox]").length;
					//由于函數(shù)執(zhí)行是按照自上而下,調(diào)用一次執(zhí)行一次的方式,所以當(dāng)?shù)谝淮闻袛噙x中按鈕為5個時,就去勾選全選按鈕,
					//當(dāng)以后每次判斷再進(jìn)來時,只會有兩種結(jié)果,一個全選是true,一個全選是false,只需要判斷全選是什么狀態(tài)就將狀態(tài)改變另一個即可。
					if(len2==len3-1 & len==false){
						$("#cballS").prop("checked",true);
					}else if(len2>=len3-1 & len==true){
						$("#cballS").prop("checked",false);
					};
					//執(zhí)行完調(diào)用總計函數(shù)
					Smon();
			};
			
			//刪除,使用函數(shù)調(diào)用完成
			function del(th){
				var aa=$(th).parent().parent().next().remove();
				$(th).parent().parent().remove();
				Smon();
			};
		
		</script>

四.小結(jié)

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

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

相關(guān)文章

  • jQuery操作文本方法介紹

    jQuery操作文本方法介紹

    這篇文章介紹了jQuery操作文本的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • jquery實現(xiàn)簡單易懂的圖片展示小例子

    jquery實現(xiàn)簡單易懂的圖片展示小例子

    圖片展示想必大家都見到過吧,下面有個不錯的例子,通俗易懂,使用jquery實現(xiàn)的,感興趣的朋友不要錯過
    2013-11-11
  • jQuery validate(submitHandler函數(shù))驗證通過發(fā)送Ajax(實例詳解)

    jQuery validate(submitHandler函數(shù))驗證通過發(fā)送Ajax(實例詳解)

    這篇文章主要介紹了jQuery validate(submitHandler函數(shù))驗證通過發(fā)送Ajax,可以用validate框架驗證元素,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-10-10
  • jQuery控制Div拖拽效果完整實例分析

    jQuery控制Div拖拽效果完整實例分析

    這篇文章主要介紹了jQuery控制Div拖拽效果完整實例,對jQuery控制拖拽效果的方法以注釋形式給予了較為詳細(xì)的介紹,便于讀者理解代碼含義,需要的朋友可以參考下
    2015-04-04
  • jQuery事件模型默認(rèn)行為執(zhí)行順序及trigger()與 triggerHandler()比較實例分析

    jQuery事件模型默認(rèn)行為執(zhí)行順序及trigger()與 triggerHandler()比較實例分析

    這篇文章主要介紹了jQuery事件模型默認(rèn)行為執(zhí)行順序及trigger()與 triggerHandler()比較,結(jié)合實例形式分析了jQuery事件模型默認(rèn)行為執(zhí)行順序相關(guān)原理,以及trigger()與 triggerHandler()的異同點比較,需要的朋友可以參考下
    2020-04-04
  • jquery分割字符串的方法

    jquery分割字符串的方法

    這篇文章主要介紹了jquery分割字符串的方法,涉及jQuery中使用split方法進(jìn)行字符串分割的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • jQuery中queue()方法用法實例

    jQuery中queue()方法用法實例

    這篇文章主要介紹了jQuery中queue()方法用法,實例分析了queue()方法的功能、定義及使用技巧,非常具有實用價值,需要的朋友可以參考下
    2014-12-12
  • jquery在項目中做復(fù)選框時遇到的一些問題筆記

    jquery在項目中做復(fù)選框時遇到的一些問題筆記

    在實踐中還是遇到了很多的問題,注意在input的checkbox中,用普通的attr屬性來判斷是不可以的,因為checked的值是checked,因此做個筆記
    2013-11-11
  • jquery tools系列 overlay 學(xué)習(xí)

    jquery tools系列 overlay 學(xué)習(xí)

    接著上次scrollable的學(xué)習(xí),今天繼續(xù)jquery tools六大功能的第四個功能——overlay的學(xué)習(xí)。
    2009-09-09
  • 簡單常用的幻燈片播放實現(xiàn)代碼

    簡單常用的幻燈片播放實現(xiàn)代碼

    幻燈片自動播放圖片是當(dāng)前網(wǎng)站比較流行的一個展示方式,這里項目需要,我自己做了一個簡單的,就不詳細(xì)講解了,代碼很簡單。直接看效果圖和代碼吧,希望對大家有所幫助
    2013-09-09

最新評論

同江市| 铅山县| 明溪县| 延津县| 偏关县| 蓬莱市| 洞口县| 屏山县| 沙雅县| 南皮县| 喀喇| 祁连县| 民乐县| 兴城市| 岳普湖县| 宁都县| 包头市| 大化| 仁寿县| 昭觉县| 普兰店市| 华容县| 正蓝旗| 贵南县| 茌平县| 壤塘县| 义乌市| 措勤县| 东阿县| 固安县| 长泰县| 临夏县| 无锡市| 南安市| 西华县| 隆昌县| 台中市| 霍邱县| 扬中市| 东台市| 二连浩特市|