åè以ä¸ä»£ç ï¼
注æéè¦å¯¼å
¥jquery.js.
<!DOCTYPE html>
<html>
<head>
<title>è´ç©è½¦----jQuery</title>
<meta charset="utf-8" />
<style type="text/css">
h1 {
text-align:center;
}
table {
margin:0 auto;
width:60%;
border:2px solid #aaa;
border-collapse:collapse;
}
table th, table td {
border:2px solid #aaa;
padding:5px;
}
th {
background-color:#eee;
}
</style>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
function add_shoppingcart(btn){//å°btn(dom)转æ¢ä¸ºjQuery对象
//å
è·åååååååä»·è¿æåºå以å¤åé¢ä½¿ç¨
var $tds = $(btn).parent().siblings();
//$tds.eq(0)æ¯jQuery对象 $tds[0]æ¯DOM对象
var name = $tds.eq(0).html();//string
var price = $tds.eq(1).html();//string
var stock = $tds.eq(3).html();//string
//æ¥çåºåæ¯å¦è¿æ<=0
if(stock <= 0){
return;
}
//æ 论è´ç©è½¦ä¸æ¯å¦æ该åå,åºåé½è¦-1
$tds.eq(3).html(--stock);
//å¨æ·»å ä¹åç¡®å®è¯¥ååå¨è´ç©è½¦ä¸æ¯å¦åå¨,è¥åå¨,åæ°é+1,è¥ä¸åå¨åå建è¡
var $trs = $("#goods>tr");
for(var i=0;i<$trs.length;i++){
var $gtds = $trs.eq(i).children();
var gName = $gtds.eq(0).html();
if(name == gName){//è¥åå¨
var num = parseInt($gtds.eq(2).children().eq(1).val());
$gtds.eq(2).children().eq(1).val(++num);//æ°é+1
//éé¢ä»æ°è®¡ç®
$gtds.eq(3).html(price*num);
return;//åé¢ä»£ç ä¸åæ§è¡
}
}
//è¥ä¸åå¨,å建å追å
var li =
"<tr>"+
"<td>"+name+"</td>"+
"<td>"+price+"</td>"+
"<td align='center'>"+
"<input type='button' value='-' onclick='decrease(this);'/> "+
"<input type='text' size='3' readonly value='1'/> "+
"<input type='button' value='+' onclick='increase(this);'/>"+
"</td>"+
"<td>"+price+"</td>"+
"<td align='center'>"+
"<input type='button' value='x' onclick='del(this);'/>"+
"</td>"+
"</tr>";
//追å å°#goodsåé¢
$("#goods").append($(li));
//æ»è®¡åè½
total();
}
//è¾
å©æ¹æ³--åå»è´ç©è½¦ä¸ç"+" "-" "x"æé®æ¯æ¾å°ç¸å
³ååæå¨td,以jQuery对象è¿å
function findStock(btn){
var name = $(btn).parent().siblings().eq(0).html();//è·ååååå
//注ætableé»è®¤æè¡åç»,è¥æ¤å¤ä½¿ç¨ $("#table1>tr:gt(0)")åæ¾ä¸å°ä»»ä½tr
var $trs = $("#table1>tbody>tr:gt(0)");
for(var i=0;i<$trs.length;i++){
var fName = $trs.eq(i).children().eq(0).html();
if(name == fName){//æ¾å°å¹é
çåå
return $trs.eq(i).children().eq(3);
}
}
}
//å¢å "+"åè½
function increase(btn){
//è·å该åååºåçæ¯å¦<=0
var $stock = findStock(btn);
var stock = $stock.html();
if(stock <= 0){
return;
}
//åºå-1
$stock.html(--stock);
//è´ç©è½¦æ°æ®æ¹å
var $td = $(btn).prev();
var num = parseInt($td.val());//number
//numæ¤æ¶ä¸ºnumberç±»å(å¨è®¡ç®æ¶ä¼èªå¨è½¬æ¢ä¸ºnumberç±»å)
$td.val(++num);
//è·ååä»·,åå 计ç®åè¦å
转æ¢ä¸ºnumberç±»å
var price = parseInt($(btn).parent().prev().html());
$(btn).parent().next().html(num*price);
//æ»è®¡åè½
total();
}
//åå°"-"åè½
function decrease(btn){
//该ååæ°é=1æ¶åä¸è½ååå°
var num = parseInt($(btn).next().val());
if(num <= 1){
return;
}
var $stock = findStock(btn);
//åºå+1
var stock = $stock.html();
$stock.html(++stock);
//ååæ°é-1
$(btn).next().val(--num);
//ä»æ°è®¡ç®éé¢
var price = parseInt($(btn).parent().prev().html());
$(btn).parent().next().html(price*num);
//æ»è®¡åè½
total();
}
//"x"å é¤æé®åè½
function del(btn){
//å°ååæ°éå½è¿åºå
var $stock = findStock(btn);
var stock = parseInt($stock.html());
var num = parseInt($(btn).parent().prev().prev().children().eq(1).val());
$stock.html(num + stock);
//æ¸
空æ¹è¡ååå表
$(btn).parent().parent().remove();
//æ»è®¡åè½
total();
}
//æ»è®¡åè½
function total(){
//è·åææè´ç©è½¦ä¸çtrs
var $trs = $("#goods tr");
var amount = 0;
for(var i=0;i<$trs.length;i++){
var money = parseInt($trs.eq(i).children().eq(3).html());
amount += money;
}
//åå
¥æ»è®¡æ
$("#total").html(amount);
}
</script>
</head>
<body>
<h1>çåç®</h1>
<table id="table1">
<tr>
<th>åå</th>
<th>åä»·(å
)</th>
<th>é¢è²</th>
<th>åºå</th>
<th>好è¯ç</th>
<th>æä½</th>
</tr>
<tr>
<td>ç½æM185é¼ æ </td>
<td>80</td>
<td>é»è²</td>
<td>5</td>
<td>98%</td>
<td align="center">
<input type="button" value="å å
¥è´ç©è½¦" onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>微软X470é®ç</td>
<td>150</td>
<td>é»è²</td>
<td>9028</td>
<td>96%</td>
<td align="center">
<input type="button" value="å å
¥è´ç©è½¦" onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>æ´å
iphone6ææºå£³</td>
<td>60</td>
<td>éæ</td>
<td>672</td>
<td>99%</td>
<td align="center">
<input type="button" value="å å
¥è´ç©è½¦" onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>èçè³æº</td>
<td>100</td>
<td>èè²</td>
<td>8937</td>
<td>95%</td>
<td align="center">
<input type="button" value="å å
¥è´ç©è½¦" onclick="add_shoppingcart(this);"/>
</td>
</tr>
<tr>
<td>é士顿Uç</td>
<td>70</td>
<td>红è²</td>
<td>482</td>
<td>100%</td>
<td align="center">
<input type="button" value="å å
¥è´ç©è½¦" onclick="add_shoppingcart(this);"/>
</td>
</tr>
</table>
<h1>è´ç©è½¦</h1>
<table>
<thead>
<tr>
<th>åå</th>
<th>åä»·(å
)</th>
<th>æ°é</th>
<th>éé¢(å
)</th>
<th>å é¤</th>
</tr>
</thead>
<tbody id="goods">
</tbody>
<tfoot>
<tr>
<td colspan="3" align="right">æ»è®¡</td>
<td id="total"></td>
<td></td>
</tr>
</tfoot>
</table>
</body>
</html>
æç»ææå¾: