This article example shares the specific code of JS to achieve simple addition and subtraction of shopping carts for your reference. The specific content is as follows 1. Write the input box as the effect of addition and subtraction. 2. The plus and minus buttons are implemented using button buttons 3. Use js to control the value of i as the value in the input box. The effect achieved: The complete code is as follows: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> body{ margin: 0; padding: 0; } .box{ border: red solid 1px; height: 40px; width: 200px; margin: 200px auto; text-align: center; padding-top: 20px; } .box input{ width: 40px; text-align: center; } #money{ border: none; text-align: left; margin-left: 2px; } ul{ list-style: none; } ul li{ display: block; float: left; vertical-align: middle; } </style> <script> window.onload = function(){ var plus = document.getElementById("plus"); var i = document.getElementById("text").value; var subtract = document.getElementById("subtract"); var money = document.getElementById("money").value; plus.onclick = function(){ i++; document.getElementById("text").value = i; document.getElementById("money").value = i*money; } subtract.onclick = function(){ if (i>0) { i--; document.getElementById("text").value = i; document.getElementById("money").value = i*money; } else{ i=0; document.getElementById("text").value = i; document.getElementById("money").value = i*money; } } } </script> </head> <body> <ul class="box"> <li><button id="plus">+</button></li> <li><input type="text" id="text" value="1"/></li> <li><button id="subtract">-</button></li> <li> ¥<input type="text" id="money" value="88"/></li> </ul> </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:
|
<<: MySql Installer 8.0.18 Visual Installation Tutorial with Pictures and Text
>>: Various problems and solutions in the process of deploying Tomcat to release projects on Linux
In the process of using Vue to develop projects, ...
What is the input type="file"? I don'...
Kernel: [root@opop ~]# cat /etc/centos-release Ce...
1. Introduction to Logrotate tool Logrotate is a ...
Use of clip-path polygon The value is composed of...
Preface The MySQL permission table is loaded into...
Download address: https://dev.mysql.com/downloads...
1. Download MySQL database and install and config...
How to delete environment variables in Linux? Use...
The first one : Copy code The code is as follows: ...
Table of contents First install wget View Help Ma...
Database backup #grammar: # mysqldump -h server-u...
This article shares the specific code of jQuery t...
Table of contents Preface 1. What is a lock? 2. L...
The appearance of a web page depends largely on i...