js 表格操作----添加删除
书名:<input type="text" id="name">
价格:<input type="text" id="price">
<button onclick="add()">添加</button>
<table id="table" width="800" border="1">
<tr>
<td>书名</td>
<td>价格</td>
<td>操作</td>
</tr>
<tr>
<td>远方的美好</td>
<td>¥99</td>
<td><button onclick="del(this)">删除</button></td>
</tr>
<tr>
<td>精通javascript</td>
<td>¥99</td>
<td><button onclick="del(this)">删除</button></td>
</tr>
</table>
<script>
function del(e){
var tr = e.parentElement.parentElement;
var index = tr.rowIndex;
var table = document.getElementById("table");
table.deleteRow(index);
}
function add(){
var table = document.getElementById("table");
}
var name = document.getElementById("name");
var price = document.getElementById("price");
var tr = table.insertRow(1);
var td1 = tr.insertCell(0);
var td2 = tr.insertCell(1);
var td3 = tr.insertCell(2);
td1.innerHTML= name.value;
td2.innerHTML= "¥"+price.value;
td3.innerHTML= '<button οnclick="del(this)">删除</button>';
price.value='';
name.value='';
</script>