用用JavaScript做简易的购物车的代码示例做简易的购物车的代码示例
主要介绍了用JavaScript做简易的购物车的代码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参
考。一起跟随小编过来看看吧
下面是我做的一个超简单的购物车,全部都是用原生js做的
css
<style>
table{
text-align: center;
}
table img{
width: 50px;
}
a {
text-decoration: none;
color: red;
}
</style>
body
<table border="1" cellspacing="0">
<thead>
<tr>
<th>全选 <input type="checkbox" id="ckAll"/></th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="ck"/></td>
<td>![](../img2/1.png)</td>
<td>3</td>
<td><input type="number" max="10" min="0" step="1" value="2"/></td>
<td><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></td>
</tr>
<tr><td><input type="checkbox" name="ck"/></td>
<td>![](../img2/2.png)</td>
<td>3</td>
<td><input type="number" max="10" min="0" step="1" value="3"/></td>
<td><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></td>
</tr>
<tr><td><input type="checkbox" name="ck"/></td>
<td>![](../img2/3.png)</td>
<td>3</td>
<td><input type="number" max="10" min="0" step="1" value="1"/></td>
<td><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="del">删除</a></td>
</tr></tbody>
<tfoot>
<tr>
<td colspan="3" align="right">总价:</td>
<td colspan="2"></td>
</tr>
</tfoot>
</table>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="dels">删除选中项</a>
js
<script>
window.onload=function(){
document.getElementById("ckAll").onclick=function(){//当点击全选的复合框时触发事件
var cks = document.getElementsByName("ck");//获取ck对象
for(var i=0;i<cks.length;i++){ //循环得到单个tr
cks[i].checked=this.checked; //得到的每个tr的点击都等于ckAll的点击
}
评论10