js to realize simple shopping cart function

js to realize simple shopping cart function

This article example shares the specific code of js to realize the simple shopping cart function for your reference. The specific content is as follows

1. Overall effect diagram

(Lights off)

(Turn on the light)

2. HTML code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shopping Cart</title>
    <link type="text/css" rel="stylesheet" href="shopping cart style.css" >
    <script src="shopping cart function.js"></script>
</head>
<body id="body" >
<button id="kg" onclick="kz()">Turn on the light</button>
<div id="cons">
    <table id="table">
        <tr>
            <th>Product Name</th>
            <th>Product unit price</th>
            <th>Product quantity</th>
            <th>Total Price</th>
        </tr>
        <tr>
            <td>Xiaomi 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)"><!--Find out who clicked through this-->
            </td>
            <td class="money">25000</td>
        </tr>
        <tr>
            <td>Lenovo 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>Men's Skin Care</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">Total amount</td>
            <td id="total">5000</td>
        </tr>
    </table>
</div>
</body>
</html>

3. CSS code

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{
    /*Define the merged display of table borders*/
    /*border-collapse: collapse;*/
    color: aquamarine;
    width: 600px;
    height: 200px;
    text-align: center;
    border-collapse: separate;border-spacing:0;/*The border-spacing property sets the distance between the borders of adjacent cells (only used in "border separate" mode). */
    table-layout:fixed;/*Fixed table layout, the horizontal layout depends only on the table width, column width, table border width, cell spacing, and has nothing to do with the content of the cell. */
 
}
#kg{
    width: 30px;
    /*border: 2px solid white;*/
    background-color: red;
    color: slateblue;
 
}

4. js code

// Addition function add(obj) {
    // Get the number of products var nums = obj.nextElementSibling.innerHTML /* Returns the value of the next sibling element node */
    if(nums>0){
        // Click to subtract nums--;
        //Replace the original value obj.nextElementSibling.innerHTML=nums;
        // Change the total price value // Get the unit price of the product var price =obj.parentElement.previousElementSibling.innerHTML;
        // Get the total price of the product var tatol = obj.parentElement.nextElementSibling.innerHTML;
        obj.parentElement.nextElementSibling.innerHTML=parseInt(nums)*parseInt(price); //parseInt converts the string into a numerical value money();
    }
 
    // console.log(nums);
 
}
// Subtraction function add2(obj){
    var nums =obj.previousElementSibling.innerHTML/*Returns the value of the previous sibling element node*/
    if(nums>=0){
        // Click to add nums++;
        //Replace the original value obj.previousElementSibling.innerHTML=nums;
        // Change the total price value // Get the unit price of the product var price =obj.parentElement.previousElementSibling.innerHTML;
        // Get the total price of the product var tatol = obj.parentElement.nextElementSibling.innerHTML;
        obj.parentElement.nextElementSibling.innerHTML=nums*price;
        money();
    }
    // console.log(nums)
}
//Get the total amount and change it function money(){
    //Get the cell of total amount var mo = document.getElementById("total");
    //Get the cell of the total price of the product var momeys=document.getElementsByClassName("money");
    //Define the value of the total amount var sum =0;
    for(var i=0;i<momeys.length;i++){
        sum=parseInt(momeys[i].innerHTML)+sum;
    }
    mo.innerHTML=sum;
    // console.log(sum)
 
}
//Control background color function kz(){
    var background = document.getElementById("body");
    var color = window.getComputedStyle(background,null).backgroundColor; //Get the background color console.log(color);
    var font = document.getElementById("table"); //font var border = document.getElementById("cons"); //border var switch1 = document.getElementById("kg"); //switch //Change background color, font color, border color if (color=="rgb(0, 0, 0)") {
        background.style.cssText="background-color: white;"; //Change CSS style font.style.cssText="color: dimgray;";
        border.style.cssText="border: 3px solid black";
        switch1.innerHTML="Turn off the light";
    }
    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";
        switch1.innerHTML="Turn on the light";
    }
 
 
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vuejs teaches you step by step to write a complete shopping cart example code
  • js shopping cart implementation ideas and code (personally feel good)
  • Writing a simple shopping cart function in JavaScript
  • Javascript detailed code to implement shopping cart function
  • js implements a simple shopping cart with pictures and codes
  • Jsp+Servlet to realize shopping cart function
  • Javascript manipulates Cookies to implement shopping cart program
  • Simple front-end js+ajax shopping cart framework (beginner's guide)
  • Native js simulation Taobao shopping cart project practice
  • js to achieve shopping cart addition and subtraction effects

<<:  Docker packages the local image and restores it to other machines

>>:  Example of MySQL slow query

Recommend

Use Docker to build a Redis master-slave replication cluster

In a cluster with master-slave replication mode, ...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

Overview of MySQL Statistics

MySQL executes SQL through the process of SQL par...

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows: Dynami...

Steps to deploy hyper-V to achieve desktop virtualization (graphic tutorial)

The hardware requirements for deploying Hyper-V a...

Example of using CASE WHEN in MySQL sorting

Preface In a previous project, the CASE WHEN sort...

MySQL concurrency control principle knowledge points

Mysql is a mainstream open source relational data...

Detailed explanation of JavaScript prototype chain

Table of contents 1. Constructors and instances 2...

CSS tips for implementing Chrome tab bar

This time let’s look at a navigation bar layout w...

Mini Program to Implement Sieve Lottery

This article example shares the specific code of ...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

Book page turning effects made with CSS3

Result:Implementation code: html <!-- Please h...

Implementation of rewrite jump in nginx

1. New and old domain name jump Application scena...

WeChat applet realizes the effect of swiping left to delete list items

This article shares the specific code for WeChat ...