Use JS to design functions
##Additional and subtractive effects
Add an onclick event to each of our value="+" and value="-"
< input type="button" value="-" onclick="minus()"/>
<input type="button" value="+" onclick="add ()"/>
Add js code in the above<script></script> tag
//Press +Button
function add(){
//Get the product quantity on the current page
var num=document.getElementById("text").value;
//Add one to the quantity and then assign it to the value attribute in the <inpue> that displays the quantity of the product
++num;
document.getElementById("text").value=num;
//Get the quantity of the product on the current page, multiply it by the retrieved price, and assign it to the page display content of the div to which the subtotal belongs
var price=document.getElementById("price ").innerHTML;
var subtotal=price*num;
document.getElementById("subtotal").innerHTML=price*num ;
}
//Press the - button
function minus(){
var num=document.getElementById("text").value;
//Determine whether the quantity of the product is less than 1, if it is less than all Assign the value to 0
if(--num<1){
document.getElementById("text").value=0;
}else{
document.getElementById("text").value=num
}
//Get the quantity of the current page, multiply it by the price, and assign it to the page display content of the div to which the subtotal belongs
//Reassign num in the case of num=-1
var num=document.getElementById("text").value;
var price=document.getElementById("price").innerHTML;
document.getElementById("subtotal").innerHTML=price*num;
}
The code is as follows:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <title>简易购物车</title> <meta charset="utf-8" /> <style> .shop{ width:400px; background-color:#f0f0f0; text-align:center; } .shop2{ text-align:center; clear:both; border:1px solid black; height:21px; } .goods{ float:left; width:100px; } .price{ float:left; width:50px; } .number{ float:left; width:110px; } .subtotal{ float:left; width:50px; margin-top:2px; } .delete{ float:left; width:35px; margin-left:5px; } .text{ width: 22px; text-align:center; } </style> <script > //按下+按钮 function add(){ //取出当前页面的数量 var num=document.getElementById("text").value; //将数量加一然后再赋值给显示数量的<inpue>中的value属性 ++num; document.getElementById("text").value=num; //取出当前页面的数量,与数量相乘,赋值给小计所属的div的页面显示内容 var price=document.getElementById("price").innerHTML; var subtotal=price*num; document.getElementById("subtotal").innerHTML=price*num; } //按下-按钮 function minus(){ var num=document.getElementById("text").value; //判断数量是不是负数 if(--num<1){ document.getElementById("text").value=0; }else{ document.getElementById("text").value=num } //取出当前页面的数量,与数量相乘,赋值给小计所属的div的页面显示内容 //给num重新赋值是放置出现num=-1情况 var num=document.getElementById("text").value; var price=document.getElementById("price").innerHTML; document.getElementById("subtotal").innerHTML=price*num; } </script> </head> <body> <!--购物车标题--> <div class="shop"> <div class="title">简易购物车</div> <div class="goods">商品</div> <div class="price">单价</div> <div class="number">数量</div> <div class="subtotal">小计</div> <div class="delete">操作</div> </div> <!--商品内容--> <div class="shop2" id="shop2"> <form> <div class="goods">小米MIX </div> <div class="price" id="price">5000</div> <div class="number"> <input type="button" value="-" onclick="minus()"/> <input type="text" value="1" class="text" id="text"/> <input type="button" value="+" onclick="add()"/> </div> <div class="subtotal" id="subtotal">5000</div> <div class="delete"><a href="#">删除</a></div> <form> </div> </body> </html>
Click The + and - signs can achieve addition and subtraction effects, but when the user wants to input numbers themselves, we cannot allow the subtotal to change accordingly, so we also need to add an out-of-focus event to the input with id="text", using isNaN () function to judge, when the user inputs a non-number, prompt the user
input to add onblur event
<input type="text" value="1" class="text" id="text" onblur="change()"/>JS code added
/ /When the user changes the number in the <input> box, the change() function is triggered after the cursor is out of focus
function change(){
//Determine whether the user input is a non-digit, and if so, remind the user
if(isNaN(document.getElementById("text").value)){
alert("Please enter a number");
document.getElementById("text").value=1;
}
## //Get the value of the input box with id="text"
var num= document.getElementById("text").value;
//Get the product price
var price=document.getElementById("price" ).innerHTML;
//Output the subtotal
document.getElementById("subtotal").innerHTML=price*num;
}
##Delete function
< div class="delete" onclick="delect('shop2')"><a href="#">delete</a></div>
JS code is as follows
##function delect(){
//Delete id= This div of "shop2"
document.body.removeChild(document.getElementById("shop2"));
}
At this point, both functions have been fully implemented. The complete code is as follows:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <title>简易购物车</title> <meta charset="utf-8" /> <style> .shop{ width:400px; background-color:#f0f0f0; text-align:center; } .shop2{ text-align:center; clear:both; border:1px solid black; height:21px; } .goods{ float:left; width:100px; } .price{ float:left; width:50px; } .number{ float:left; width:110px; } .subtotal{ float:left; width:50px; margin-top:2px; } .delete{ float:left; width:35px; margin-left:5px; } .text{ width: 22px; text-align:center; } </style> <script > //按下+按钮 function add(){ //取出当前页面的数量 var num=document.getElementById("text").value; //将数量加一然后再赋值给显示数量的<inpue>中的value属性 ++num; document.getElementById("text").value=num; //取出当前页面的数量,与数量相乘,赋值给小计所属的div的页面显示内容 var price=document.getElementById("price").innerHTML; var subtotal=price*num; document.getElementById("subtotal").innerHTML=price*num; } //按下-按钮 function minus(){ var num=document.getElementById("text").value; //判断数量是不是负数 if(--num<1){ document.getElementById("text").value=0; }else{ document.getElementById("text").value=num } //取出当前页面的数量,与数量相乘,赋值给小计所属的div的页面显示内容 //给num重新赋值是放置出现num=-1情况 var num=document.getElementById("text").value; var price=document.getElementById("price").innerHTML; document.getElementById("subtotal").innerHTML=price*num; } //用户在<input>框中改变数字时,光标失焦后触发change()函数 function change(){ //判断用户输入的是否为非数字,是则提醒用户 if(isNaN(document.getElementById("text").value)){ alert("请输入数字"); document.getElementById("text").value=1; } //取得id="text"的input框的value值 var num=document.getElementById("text").value; //取得商品价格 var price=document.getElementById("price").innerHTML; //将小计输出出去 document.getElementById("subtotal").innerHTML=price*num; } function delect(){ //删除id="shop2"的这个div document.body.removeChild(document.getElementById("shop2")); } </script> </head> <body> <!--购物车标题--> <div class="shop"> <div class="title">简易购物车</div> <div class="goods">商品</div> <div class="price">单价</div> <div class="number">数量</div> <div class="subtotal">小计</div> <div class="delete">操作</div> </div> <!--商品内容--> <div class="shop2" id="shop2"> <form> <div class="goods">小米MIX </div> <div class="price" id="price">5000</div> <div class="number"> <input type="button" value="-" onclick="minus()"/> <input type="text" value="1" class="text" id="text" onblur="change()"/> <input type="button" value="+" onclick="add()"/> </div> <div class="subtotal" id="subtotal">5000</div> <div class="delete" onclick="delect()"><a href="#">删除</a></div> <form> </div> </body> </html>