<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车的实现</title>
<style type="text/css">
table {
width: 400px;
border-collapse: collapse;
margin: auto;
}
td, th {
text-align: center;
height: 30px;
}
.container {
width: 400px;
margin: auto;
text-align: right;
}
img {
width: 100px;
height: 100px;
}
th {
background-color: lightgray;
}
tr:hover {
background-color: lightyellow;
}
</style>
<script type="text/javascript" src="../js/jquery-3.3.1.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<div class="container">
<table border="1">
<tbody id="cart">
<tr>
<th>
图片
</th>
<th>
商品名
</th>
<th>
操作
</th>
</tr>
<tr>
<td><img src="../img/sx.jpg"/></td>
<td>
三星Note7
</td>
<td>
<!--this表示当前按钮-->
<input type="button" value="删除" onclick="deleteRow(this)">
</td>
</tr>
<tbody>
</table>
<br/>
商品名:
<input type="text" id="productName" value=""/>
<input type="button" value="添加到购物车" id="addRow"/>
</div>
</body>
<script type="text/javascript">
function deleteRow(obj){
obj.remove();
}
//添加单击事件
$("#addRow").click(function () {
//获取输入框的值
var productName = $("#productName").val().trim();
if(productName==""){
alert("商品名不能为空!")
return;
}
//追加到购物车后面
$("#cart").append("<tr>\n" +
" <td><img src=\"../img/sx.jpg\"/></td>\n" +
" <td>\n" +
" "+productName+"\n" +
" </td>\n" +
" <td>\n" +
" <!--this表示当前按钮-->\n" +
" <input type=\"button\" value=\"删除\" onclick=\"deleteRow(this)\">\n" +
" </td>\n" +
" </tr>");
$("#productName").val("");
});
</script>
</html>
jQuery案例 : 模拟购物车