溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

js怎樣實(shí)現(xiàn)簡易購物車功能

發(fā)布時(shí)間:2021-10-11 09:22:09 來源:億速云 閱讀:150 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)js怎樣實(shí)現(xiàn)簡易購物車功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

一.整體效果圖

(關(guān)燈下)

js怎樣實(shí)現(xiàn)簡易購物車功能

 (開燈下)

js怎樣實(shí)現(xiàn)簡易購物車功能

二.HTML代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>購物車</title>
    <link type="text/css" rel="stylesheet" href="購物車樣式.css" >
    <script src="購物車功能.js"></script>
</head>
<body id="body" >
<button id="kg" onclick="kz()">開燈</button>
<div id="cons">
    <table id="table">
        <tr>
            <th>產(chǎn)品名稱</th>
            <th>產(chǎn)品單價(jià)</th>
            <th>產(chǎn)品數(shù)量</th>
            <th>總價(jià)</th>
        </tr>
        <tr>
            <td>小米11</td>
            <td >5000</td>
            <td>
                <input type="button" value="-" onclick="add(this)">
                <span class="num">5</span>
                <input type="button" value="+" onclick="add2(this)"><!--通過this找到點(diǎn)擊的是誰-->
            </td>
            <td class="money">25000</td>
        </tr>
        <tr>
            <td>聯(lián)想Y9000</td>
            <td>10000</td>
            <td>
                <input type="button" value="-" onclick="add(this)">
                <span class="num">1</span>
                <input type="button" value="+" onclick="add2(this)">
            </td>
            <td class="money">10000</td>
        </tr>
        <tr>
            <td>男士護(hù)膚</td>
            <td>200</td>
            <td>
                <input type="button" value="-" onclick="add(this)">
                <span class="num">1</span>
                <input type="button" value="+" onclick="add2(this)">
            </td>
            <td class="money">200</td>
        </tr>
        <tr>
            <td colspan="3">總金額</td>
            <td id="total">5000</td>
        </tr>
    </table>
</div>
</body>
</html>

三.CSS代碼

table,th,td,tr{
    border: 5px solid slateblue;
    border-radius: 10px;
 
             }
#cons{
    border: 3px solid #FFFFFF;
    width: 600px;
    padding: 5px;
    border-radius: 10px;
    margin: 200px auto;
}
#body{
    background-color: black;
}
 
table{
    /*定義表格邊框合并顯示*/
    /*border-collapse: collapse;*/
    color: aquamarine;
    width: 600px;
    height: 200px;
    text-align: center;
    border-collapse: separate;border-spacing:0;/*border-spacing 屬性設(shè)置相鄰單元格的邊框間的距離(僅用于“邊框分離”模式)。*/
    table-layout:fixed;/*固定表格布局,水平布局僅僅取決于表格寬度、列寬度、表格邊框?qū)挾?、單元格間距、而與單元格的內(nèi)容無關(guān)。*/
 
}
#kg{
    width: 30px;
    /*border: 2px solid white;*/
    background-color: red;
    color: slateblue;
 
}

四. js代碼

// 加法
function add(obj) {
    // 獲取商品的數(shù)量
    var nums=obj.nextElementSibling.innerHTML/*返回的是后一個(gè)兄弟元素節(jié)點(diǎn)的值*/
    if(nums>0){
        // 點(diǎn)擊減一
        nums--;
        // 替換原來的值
        obj.nextElementSibling.innerHTML=nums;
        // 改變總價(jià)的值
        //獲取商品單價(jià)
        var price =obj.parentElement.previousElementSibling.innerHTML;
        // 獲取商品總價(jià)
        var tatol= obj.parentElement.nextElementSibling.innerHTML;
        obj.parentElement.nextElementSibling.innerHTML=parseInt(nums)*parseInt(price);//parseInt 將字符串轉(zhuǎn)成數(shù)值
        money();
    }
 
    // console.log(nums);
 
}
// 減法
function add2(obj){
    var nums =obj.previousElementSibling.innerHTML/*返回的是前一個(gè)兄弟元素節(jié)點(diǎn)的值*/
    if(nums>=0){
        // 點(diǎn)擊加一
        nums++;
        // 替換原來的值
        obj.previousElementSibling.innerHTML=nums;
        // 改變總價(jià)的值
        //獲取商品單價(jià)
        var price =obj.parentElement.previousElementSibling.innerHTML;
        // 獲取商品總價(jià)
        var tatol= obj.parentElement.nextElementSibling.innerHTML;
        obj.parentElement.nextElementSibling.innerHTML=nums*price;
        money();
    }
    // console.log(nums)
}
//獲取總金額的值,并改變它
function money(){
    //獲取總金額的單元格
    var mo =document.getElementById("total");
    //獲取商品總價(jià)的單元格
    var momeys=document.getElementsByClassName("money");
    //定義總金額的值
    var sum =0;
    for(var i=0;i<momeys.length;i++){
        sum=parseInt(momeys[i].innerHTML)+sum;
    }
    mo.innerHTML=sum;
    // console.log(sum)
 
}
//控制背景顏色
function kz(){
    var background=document.getElementById("body");
    var color= window.getComputedStyle(background,null).backgroundColor;//獲取背景顏色
    console.log(color);
    var font =document.getElementById("table");//字體
    var border =document.getElementById("cons");//邊框
    var switch2=document.getElementById("kg");//開關(guān)
    //更換背景顏色,和字體顏色,邊框顏色
    if(color=="rgb(0, 0, 0)"){
        background.style.cssText="background-color: white;";//更改css樣式
        font.style.cssText="color: dimgray;";
        border.style.cssText="border: 3px solid black";
        switch2.innerHTML="關(guān)燈";
    }
    else if(color=="rgb(255, 255, 255)"){
        background.style.cssText="background-color: black;";
        font.style.cssText="color: aquamarine;";
        border.style.cssText="border: 3px solid #FFFFFF";
        switch2.innerHTML="開燈";
    }
 
 
}

感謝各位的閱讀!關(guān)于“js怎樣實(shí)現(xiàn)簡易購物車功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI