JavaScript ?? ???? ???? JS ?? ??
JS? ???? ?? ?????
?? ? ?? ??
value="+" ? value="-"? onclick ???? ?????
<input type="button" value="-" onclick="minus()"/>
<input type="button" value="+" onclick="add()"/ >
?? <script></script> ??? js ??? ?????
//+ ??? ????
function add(){
//?? ???? ?? ????? Quantity
var num=document.getElementById("text").value;
//??? 1? ?? ?? ???? <inpue> ?? ??
+ +num;
document.getElementById("text").value=num;
//?? ???? ?? ??? ??? ?? ????. ??? ??? ??? ?? div? ??? ??? ?????. Content
var Price=document.getElementById("price").innerHTML;
var subtotal=price*num;
document.getElementById("subtotal").innerHTML=price*num;
}
//- ??? ????
function minus(){
?? ??= document.getElementById("text").value;
//?? ?? ?? ??? 1 ??? ??, 1 ??? ?? ?? ?? 0
? ?????. if(--num<1){
document.getElementById("text").value=0;
}else{
document.getElementById("text").value=num
}
//?? ???? ??? ?? ??? ??? ??? ?? div? ??? ?? ??? ??
//??? ?? ?? num=-1
var num=document.getElementById("text").value;
var Price=document.getElementById("price") innerHTML;
??? ??? ????. getElementById("subtotal").innerHTML=price*num;
}
??? ??? ????.
<!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>簡(jiǎn)易購(gòu)物車(chē)</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(){ //取出當(dāng)前頁(yè)面的數(shù)量 var num=document.getElementById("text").value; //將數(shù)量加一然后再賦值給顯示數(shù)量的<inpue>中的value屬性 ++num; document.getElementById("text").value=num; //取出當(dāng)前頁(yè)面的數(shù)量,與數(shù)量相乘,賦值給小計(jì)所屬的div的頁(yè)面顯示內(nèi)容 var price=document.getElementById("price").innerHTML; var subtotal=price*num; document.getElementById("subtotal").innerHTML=price*num; } //按下-按鈕 function minus(){ var num=document.getElementById("text").value; //判斷數(shù)量是不是負(fù)數(shù) if(--num<1){ document.getElementById("text").value=0; }else{ document.getElementById("text").value=num } //取出當(dāng)前頁(yè)面的數(shù)量,與數(shù)量相乘,賦值給小計(jì)所屬的div的頁(yè)面顯示內(nèi)容 //給num重新賦值是放置出現(xiàn)num=-1情況 var num=document.getElementById("text").value; var price=document.getElementById("price").innerHTML; document.getElementById("subtotal").innerHTML=price*num; } </script> </head> <body> <!--購(gòu)物車(chē)標(biāo)題--> <div class="shop"> <div class="title">簡(jiǎn)易購(gòu)物車(chē)</div> <div class="goods">商品</div> <div class="price">單價(jià)</div> <div class="number">數(shù)量</div> <div class="subtotal">小計(jì)</div> <div class="delete">操作</div> </div> <!--商品內(nèi)容--> <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>
+ ? - ??? ???? ?? ?????. ?? ??? ??? ???? ?? ??? ????? ?? ??? ?? ?? ????? ??? ? ???? id="text"? ??? ??? ?? ?? ???? ???? isNaN( ) ???? ??? ?? ?? ???? ?????
input? onblur ???? ????? ???? ?????.
<input type="text" value="1" class="text" id= "text" onblur="change( )"/>
JS ?? ???
//???? <input> ??? ??? ???? ??? ??? ? ??() ??? ??????. ???
?? ??(){
//??? ??? ??? ??? ?????. ???? ????? ??????
if(isNaN(document.getElementById("text").value)){
Alert("?????. a number") ;
document.getElementById("text").value=1;
}
//id="text"? ?? ?? ? ????
var num=document.getElementById("text").value;
//?? ?? ????
var Price=document.getElementById("price").innerHTML;
//?? subtotal
document.getElementById("subtotal").innerHTML=price*num;
}
?? ??
?? <div&g t; ?? ??? <? ???? div
<div class="delete" onclick="delect('shop2')"><a href="#">Delete<? onclick ???? ??????. ;/ a></div>
JS ??? ??? ????
function delect(){
//id? "shop2"? div? ??
document.body.removeChild(document.getElementById("shop2"));
}
? ???? ? ??? ?? ???????.
<!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>簡(jiǎn)易購(gòu)物車(chē)</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(){ //取出當(dāng)前頁(yè)面的數(shù)量 var num=document.getElementById("text").value; //將數(shù)量加一然后再賦值給顯示數(shù)量的<inpue>中的value屬性 ++num; document.getElementById("text").value=num; //取出當(dāng)前頁(yè)面的數(shù)量,與數(shù)量相乘,賦值給小計(jì)所屬的div的頁(yè)面顯示內(nèi)容 var price=document.getElementById("price").innerHTML; var subtotal=price*num; document.getElementById("subtotal").innerHTML=price*num; } //按下-按鈕 function minus(){ var num=document.getElementById("text").value; //判斷數(shù)量是不是負(fù)數(shù) if(--num<1){ document.getElementById("text").value=0; }else{ document.getElementById("text").value=num } //取出當(dāng)前頁(yè)面的數(shù)量,與數(shù)量相乘,賦值給小計(jì)所屬的div的頁(yè)面顯示內(nèi)容 //給num重新賦值是放置出現(xiàn)num=-1情況 var num=document.getElementById("text").value; var price=document.getElementById("price").innerHTML; document.getElementById("subtotal").innerHTML=price*num; } //用戶在<input>框中改變數(shù)字時(shí),光標(biāo)失焦后觸發(fā)change()函數(shù) function change(){ //判斷用戶輸入的是否為非數(shù)字,是則提醒用戶 if(isNaN(document.getElementById("text").value)){ alert("請(qǐng)輸入數(shù)字"); document.getElementById("text").value=1; } //取得id="text"的input框的value值 var num=document.getElementById("text").value; //取得商品價(jià)格 var price=document.getElementById("price").innerHTML; //將小計(jì)輸出出去 document.getElementById("subtotal").innerHTML=price*num; } function delect(){ //刪除id="shop2"的這個(gè)div document.body.removeChild(document.getElementById("shop2")); } </script> </head> <body> <!--購(gòu)物車(chē)標(biāo)題--> <div class="shop"> <div class="title">簡(jiǎn)易購(gòu)物車(chē)</div> <div class="goods">商品</div> <div class="price">單價(jià)</div> <div class="number">數(shù)量</div> <div class="subtotal">小計(jì)</div> <div class="delete">操作</div> </div> <!--商品內(nèi)容--> <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>