Implementing a shopping cart with native JavaScript

Implementing a shopping cart with native JavaScript

This article shares the specific code of JavaScript to implement the shopping cart for your reference. The specific content is as follows

Effect:

Code:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style>
 *{
 margin: 0;
 padding:0;
 }
 .box{
 width:600px;
 margin: 10px auto;
 background: url(img/g.jpg) ;
 height:500px;
 }
 td{
 text-align: center;
 font-size: 30px;
 color:orange;
 }
 button{
 width:150px;
 border:0;
 border-radius: 5px;
 height:30px;
 background-color: dodgerblue;
 }
 
 /*.trl:hover{
 background:pink;
 }*/
 
 </style>
 </head>
 <body>
 <div class="box">
 <br />
 <button>Delete All</button>
 <button>Select Delete</button>
 <br>
 <br>
 <input type="text" value="Please enter keywords" />
 <input type="button" value="Search" />
 
 <br>
 <br>
 <table width='600' border="1" cellspacing="0">
 <tr>
  <th><input type="checkbox" class="qx" />Select all</th>
  <th>Products</th>
  <th>Price</th>
  <th>Serial Number</th>
  <th>Origin</th>
  <th>Brand</th>
  <th>Operation</th>
 </tr>
 <tr class="trl">
  <td><input type="checkbox" class="dx" /></td>
  <td class="shop">Durian</td>
  <td>20 yuan</td>
  <td class="ind">1</td>
  <td>Hainan</td>
  <td>Anpai</td>
  <td onclick="del(this)">Delete</td>
 </tr>
 <tr class="trl">
  <td><input type="checkbox" class="dx" /></td>
  <td class="shop">Apple</td>
  <td>20 yuan</td>
  <td class="ind">2</td>
  <td>Hainan</td>
  <td>Anpai</td>
  <td onclick="del(this)">Delete</td>
 </tr>
 <tr class="trl">
  <td><input type="checkbox" class="dx" /></td>
  <td class="shop">Strawberry</td>
  <td>20 yuan</td>
  <td class="ind">3</td>
  <td>Hainan</td>
  <td>Anpai</td>
  <td onclick="del(this)">Delete</td>
 </tr>
 <tr class="trl">
  <td><input type="checkbox" class="dx" /></td>
  <td class="shop">Banana</td>
  <td>20 yuan</td>
  <td class="ind">4</td>
  <td>Hainan</td>
  <td>Anpai</td>
  <td onclick="del(this)">Delete</td>
 </tr>
 </table>
 </div>
 <script>
//Select all or deselect none var qx=document.getElementsByClassName('qx')[0];
   var dx = document.getElementsByClassName("dx");
   qx.onclick=function(){
    for(var i=0;i<dx.length;i++){
    dx[i].checked=qx.checked
    }
   }
 //Delete all var btn=document.getElementsByTagName("button");
   var tr = document.getElementsByTagName('tr');
   var tbody = document.getElementsByTagName("tbody")[0];
   btn[0].onclick=function(){
    for(var i=1;i<tr.length;i++){
     tbody.removeChild(tr[i]);
     i--;
    }       
   }
 //Select and delete var dx=document.getElementsByClassName('dx');
   btn[1].onclick=function(){
   for(var i=0;i<dx.length;i++){
    if(dx[i].checked==true){
    tbody.removeChild(dx[i].parentNode.parentNode)
    i--;
    indChange();
    }
   }
   }
 // Clear the text box to search and change the color var input = document.getElementsByTagName('input');
  
  input[0].onfocus=function(){
  this.value=""
  }
  
  var shop = document.getElementsByClassName('shop');
  
  input[1].onclick=function(){
   for(var i=0;i<shop.length;i++){
   
   if(shop[i].innerHTML==input[0].value){
   for(var j=0;j<shop.length;j++){
   shop[j].parentNode.style.background=""
   }
   shop[i].parentNode.style.background="yellow"
   }
   }
  }
  
// 
//Move in and out hover 
  for(var i=1;i<tr.length;i++){
  tr[i].onmouseover=function(){
  this.style.background="pink"
  }
  tr[i].onmouseout=function(){
  this.style.background=""
  }
  
  }

// Single row deletion (serial number)
// Parent element.removeChild(child element) tbody delete tr
 function del(t){
  tbody.removeChild(t.parentNode);
  indChange();
 }
 
 
 //Serial number function indChange(){
  var ind = document.getElementsByClassName("ind");
  for(var i=0;i<ind.length;i++){
  ind[i].innerHTML=i+1;
  }
 }
 
 
 </script>
 </body>
</html>

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:
  • JS realizes the basic functions of shopping cart
  • Native js to realize shopping cart function
  • Native js to implement shopping cart
  • js to add and subtract the number of items in the shopping cart
  • Vue.js implements simple shopping cart function
  • 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

<<:  JavaScript Html to implement the mobile red envelope rain function page

>>:  JS code to achieve page switching effect

Blog    

Recommend

Exploring the practical value of the CSS property *-gradient

Let me first introduce an interesting property - ...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Detailed explanation of DOM DIFF algorithm in react application

Table of contents Preface What is VirtualDOM? Rea...

Detailed explanation of writing and using Makefile under Linux

Table of contents Makefile Makefile naming and ru...

Summary of MySQL stored procedure permission issues

MySQL stored procedures, yes, look like very rare...

Web page header optimization suggestions

Logo optimization: 1.The logo image should be as ...

MySQL 5.7.21 installation and password configuration tutorial

MySQL5.7.21 installation and password setting tut...

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

Implementation of vue3.0+vant3.0 rapid project construction

Table of contents 1. Project Construction 2. Vue3...

JavaScript to achieve a simple carousel effect

What is a carousel? Carousel: In a module or wind...