书名:<input type="text" id="shu">
价格:<input type="text" id="jia">
<button onclick="add()">添加</button>
<table width="300px" border="1px" id="bg">
<tr>
<td>书名:</td>
<td>价格:</td>
<td>操作</td>
</tr>
<tr>
<td>《白夜行》</td>
<td>¥30</td>
<td><button onclick="del(this)">x</button></td>
</tr>
</table>
<script>
var shu = document.getElementById("shu");
var jia = document.getElementById("jia");
var bg = document.getElementById("bg")
function add(){
var tr = bg.insertRow(1);
var td = tr.insertCell(0);
var td1 = tr.insertCell(1);
var td2 = tr.insertCell(2);
td.innerHTML = shu.value;
td1.innerHTML = jia.value;
td2.innerHTML = '<button onclick="del(this)">x</button>';
shu.value = "";
jia.value = "";
}
function del(a){
var b = a.parentElement.parentElement;
var c = b.rowIndex;
bg.deleteRow(c);
}
</script>
利用javascript实现简单的图书增减操作
最新推荐文章于 2022-11-27 17:25:05 发布