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

js實(shí)現(xiàn)購(gòu)物車加減以及價(jià)格計(jì)算功能

 更新時(shí)間:2021年08月19日 08:53:48   作者:Sestid  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)購(gòu)物車加減以及價(jià)格計(jì)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)購(gòu)物車加減以及價(jià)格計(jì)算的具體代碼,供大家參考,具體內(nèi)容如下

需求說(shuō)明:

1、單擊“半閉”按鈕時(shí),關(guān)閉當(dāng)前頁(yè)面購(gòu)物車頁(yè)面
2、單擊“移入收藏”彈出收藏提示
3、單擊“刪除”彈出確認(rèn)刪除提示
4、單擊“結(jié)算”按鈕,彈出結(jié)算信息頁(yè)面窗口
5、自動(dòng)計(jì)算商品總計(jì)
6、單擊“刪除”按鈕,使用parentNode訪問(wèn)當(dāng)前節(jié)點(diǎn)的父親節(jié)點(diǎn)等,使用removeChild( )刪除當(dāng)前商品

效果圖:

代碼:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>完善當(dāng)當(dāng)購(gòu)物車頁(yè)面</title>
    <style type="text/css">
     body,ul,li,div,p,h1,h2,ol{margin: 0;padding: 0;}
ul,li,ol{list-style: none;}
.content{width: 810px; margin: 0 auto;  font-family: "微軟雅黑";}
.logo{margin: 10px 0;}
.logo span{
    display: inline-block;
    float: right;
    width: 60px;
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    background: #ff0000;
    color: #ffffff;
    text-align: center;
    border-radius: 10px;
    margin-top: 5px;
    margin-right: 10px;
    cursor: pointer;
    font-weight: bold;
}
.cartList{
    /*background: url("../image/02.jpg") no-repeat;*/
    /*height: 414px;*/
    overflow: hidden;
}
.cartList ul{
    display: flex;
    justify-content: space-between;
    /*float: right;*/
    /*width: 450px;*/
}
.cartList ul:nth-of-type(1){
    display: flex;
    margin-top: 125px;
}
.cartList ul:nth-of-type(2){
    margin: 20px 0;
}
.cartList ul li{
    font-family: "微軟雅黑";
    font-size: 12px;
    color: #666666;
    text-align: center;
    line-height: 25px;
    /*float: left;*/
}
.cartList ul li input[name="price"]{
    border: none;
    background: transparent;
    width: 45px;
    text-align: center;
}
.cartList ul li input[name="amount"]{
    width: 45px;
    text-align: center;
    border: 1px solid #999999;
    border-left: none;
    border-right: none;
    height: 21px;
}
.cartList ul li input[name="minus"],.cartList ul li input[name="plus"]{
    height: 25px;
    border: 1px #999999 solid;
    width: 25px;
    text-align: center;
}
.cartList ul li:nth-of-type(1){width: 130px;}
.cartList ul li:nth-of-type(2){width: 100px;}
.cartList ul li:nth-of-type(3){width: 130px;}
.cartList ul li p{cursor: pointer;}
.cartList ol{
    float: right;
    clear: both;
    margin-top: 40px;
}
.cartList ol li{
    float: left;
}
.cartList ol li:nth-of-type(1){
    color: #ff0000;
    width: 80px;
    height: 35px;
    line-height: 35px;
    text-align: center;
}
.cartList ol li span{display: inline-block;
    float: right;
    width: 80px;
    height: 35px;
    line-height: 35px;
    font-size: 14px;
    font-family: "微軟雅黑";
    background: #ff0000;
    color: #ffffff;
    text-align: center;
    /*margin-top: 5px;*/
    /*margin-right: 15px;*/
    cursor: pointer;
    font-weight: bold;}
 
    </style>
</head>
 
<!--onload,在加載時(shí)計(jì)算好原始金額-->
<body onload="total()">
 
<div class="content">
    <div class="logo">
        <span onclick="javascript:if (confirm('確認(rèn)要關(guān)閉嗎'))window.close() ">關(guān)閉</span>
    </div>
    <div class="cartList">
        <ul>
            <li>商品信息</li>
            <li>商品圖片</li>
            <li>單價(jià)(元)</li>
            <li>數(shù)量</li>
            <li>金額(元)</li>
            <li>操作</li>
        </ul>
        <ul style="display: flex;justify-content: space-between;align-items: center" id="first">
            <li>《平凡的世界》</li>
            <li><img src="./img/1.png" alt="" width="50" height="50"></li>
            <li>¥<input type="text" name="price" value="21.90"></li>
            <li><input type="button" name="minus" value="-" onclick="minus(0)"><input type="text" name="amount" value="1"><input type="button" name="plus" value="+" onclick="plus(0)" ></li>
            <li id="price0">¥21.90</li>
            <li><p onclick="save()">移入收藏</p><p onclick="delete1()">刪除</p></li>
        </ul>
        <ul style="display: flex;justify-content: space-between;align-items: center; margin: 20px 0;">
            <li>《昆蟲記》</li>
            <li><img src="./img/2.png" alt="" width="50" height="50"></li>
            <li>¥<input type="text" name="price" value="24.00"></li>
            <li><input type="button" name="minus" value="-" onclick="minus(1)"><input type="text" name="amount" value="1"><input type="button" name="plus" value="+" onclick="plus(1)"></li>
            <li id="price1">¥24.00</li>
            <li><p onclick="save()">移入收藏</p><p onclick="delete1()">刪除</p></li>
        </ul>
        <ol>
            <li id="totalPrice">&nbsp;</li>
            <li><span>結(jié)算</span></li>
        </ol>
    </div>
</div>
</body>
</html>
 
<script>
    //減法
    function minus(index) {
        //獲取當(dāng)前數(shù)量的值
        var amounts=document.getElementsByName("amount");
 
        //得到第一個(gè)amount的元素的value屬性的值
        var count=parseInt(amounts[index].value); //數(shù)量加1
 
        if (count<=1){
            alert("不能再減了,快沒(méi)了??!");
        } else {
            //得到第一個(gè)amount的元素的value屬性的值
            var count=parseInt(amounts[index].value)-1; //數(shù)量加1
 
            //重新把count的值綁定在數(shù)量文本框里
            amounts[index].value=count;
            var prices=document.getElementsByName("price");
            var price=parseFloat(prices[index].value);
            //乘以Math.pow(10,2)的原因?yàn)楸苊馐д?
            var totalMoney=((price*Math.pow(10,2))*count)/Math.pow(10,2);
 
            document.getElementById("price"+index).innerHTML="¥:"+totalMoney;
        }
 
        total();
 
    }
 
    //加法
    function plus(index) {
 
        //獲取當(dāng)前數(shù)量的值
        var amounts=document.getElementsByName("amount");
 
        //得到第一個(gè)amount的元素的value屬性的值
        var count=parseInt(amounts[index].value)+1; //數(shù)量加1
 
        //重新把count的值綁定在數(shù)量文本框里
        amounts[index].value=count;
 
        //當(dāng)前操作端口的價(jià)格也要重新計(jì)算
        //得到當(dāng)前端口的單價(jià)
        var prices=document.getElementsByName("price");
        var price=parseFloat(prices[index].value);
        //乘以Math.pow(10,2)的原因?yàn)楸苊馐д?
        var totalMoney=((price*Math.pow(10,2))*count)/Math.pow(10,2);
 
        //把當(dāng)前價(jià)格顯示在文本中
        document.getElementById("price"+index).innerHTML="¥:"+totalMoney;
 
        total();
    }
 
 
    //求總金額
 
    function total() {
 
        //得到所有的數(shù)量
        var counts=document.getElementsByName("amount");
 
        //得到所有的單價(jià)
        var prices=document.getElementsByName("price");
 
        var sumMoney=0;
 
        for (var i=0;i<counts.length;i++){
 
            //乘以Math.pow(10,2)的原因?yàn)楸苊馐д?
            sumMoney+=(parseFloat(prices[i].value)*Math.pow(10,2)*parseInt(counts[i].value)/Math.pow(10,2));
        }
 
        //把總金額顯示再指定的元素中
        document.getElementById("totalPrice").innerHTML="¥:"+sumMoney;
 
    }
 
 
    //加入收藏
    function save() {
        if (confirm("確認(rèn)要收藏嗎?")){
            alert("收藏成功!");
        }
 
    }
    //刪除
    function delete1() {
        if (confirm("確認(rèn)要?jiǎng)h除嗎?")) {
            var del=document.getElementById("first");
            del.parentNode.removeChild(del);
            alert("刪除成功??!");
        }
    }
</script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • window.location.hash 屬性使用說(shuō)明

    window.location.hash 屬性使用說(shuō)明

    location是javascript里邊管理地址欄的內(nèi)置對(duì)象,比如location.href就管理頁(yè)面的url,用location.href=url就可以直接將頁(yè)面重定向url。而location.hash則可以用來(lái)獲取或設(shè)置頁(yè)面的標(biāo)簽值。
    2010-03-03
  • JS定時(shí)器不可靠的原因及解決方案

    JS定時(shí)器不可靠的原因及解決方案

    JavaScript中所有的任務(wù)分為同步任務(wù)與異步任務(wù),同步任務(wù),顧名思義就是立即執(zhí)行的任務(wù),它一般是直接進(jìn)入到主線程中執(zhí)行,這篇文章主要介紹了JS定時(shí)器不可靠的原因及解決方案,需要的朋友可以參考下
    2022-01-01
  • JavaScript 大數(shù)據(jù)相加的問(wèn)題

    JavaScript 大數(shù)據(jù)相加的問(wèn)題

    寫一個(gè)函數(shù)處理大數(shù)據(jù)的相加問(wèn)題,所謂的大數(shù)據(jù)是指超出了整型,長(zhǎng)整型之類的常規(guī)數(shù)據(jù)類型表示范圍的數(shù)據(jù)。實(shí)現(xiàn)語(yǔ)言不限。
    2011-08-08
  • JavaScript 解析讀取XML文檔 實(shí)例代碼

    JavaScript 解析讀取XML文檔 實(shí)例代碼

    應(yīng)項(xiàng)目之需求,需用JS操作XML文檔,遂上網(wǎng)查資料,感覺(jué)這篇文章還不錯(cuò),特轉(zhuǎn)載到此地,與大家共享!
    2009-07-07
  • 簡(jiǎn)短幾句 通俗解釋javascript的閉包

    簡(jiǎn)短幾句 通俗解釋javascript的閉包

    什么是閉包呢?閉包就是函數(shù)實(shí)例執(zhí)行過(guò)程中動(dòng)態(tài)產(chǎn)生一個(gè)沒(méi)有被釋放資源的棧區(qū),也是一個(gè)預(yù)執(zhí)行的過(guò)程。
    2011-01-01
  • 原生js封裝二級(jí)城市下拉列表的實(shí)現(xiàn)代碼

    原生js封裝二級(jí)城市下拉列表的實(shí)現(xiàn)代碼

    下面小編就為大家?guī)?lái)一篇原生js封裝二級(jí)城市下拉列表的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • js實(shí)現(xiàn)頁(yè)面導(dǎo)航層級(jí)指示效果

    js實(shí)現(xiàn)頁(yè)面導(dǎo)航層級(jí)指示效果

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)頁(yè)面導(dǎo)航層級(jí)指示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Javascript實(shí)現(xiàn)快速排序(Quicksort)的算法詳解

    Javascript實(shí)現(xiàn)快速排序(Quicksort)的算法詳解

    排序算法(Sorting algorithm)是計(jì)算機(jī)科學(xué)最古老、最基本的課題之一,要想成為合格的程序員,就必須理解和掌握各種排序算法。
    2015-09-09
  • js傳參數(shù)受特殊字符影響錯(cuò)誤的解決方法

    js傳參數(shù)受特殊字符影響錯(cuò)誤的解決方法

    js傳參數(shù)受特殊字符影響錯(cuò)誤,如果不做處理,會(huì)產(chǎn)品傳參錯(cuò)誤,encodeURI 方法返回一個(gè)編碼的URI,可以有效解決此問(wèn)題,感興趣的朋友可以參考下
    2013-10-10
  • 簡(jiǎn)單實(shí)現(xiàn)js拖拽效果

    簡(jiǎn)單實(shí)現(xiàn)js拖拽效果

    這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)js拖拽效果,很詳細(xì)的js拖拽效果實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論

大港区| 铅山县| 锦屏县| 普兰店市| 伊春市| 永靖县| 甘泉县| 积石山| 塔城市| 南川市| 凉城县| 平江县| 金山区| 罗田县| 库伦旗| 恩施市| 庆安县| 民县| 梅河口市| 礼泉县| 会昌县| 辰溪县| 正定县| 鸡泽县| 松滋市| 奎屯市| 颍上县| 进贤县| 合肥市| 巴彦淖尔市| 乌海市| 阳曲县| 汉阴县| 高淳县| 遵化市| 疏勒县| 阿鲁科尔沁旗| 明溪县| 鄂伦春自治旗| 阿拉善盟| 临江市|