不能眼高手低啊
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<script type="text/javascript">?
??
? ? ? window.onload = function(){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
? ? ? ? ?changeColor();
}
? ? ?function changeColor(){
var trs = document.getElementsByTagName("tr");
for(let i=1;i<trs.length;i++){
trs[i].onmouseover = function(){
this.style.background = "#ADD8E6";
};
trs[i].onmouseout = function(){
this.style.background = "";
}
}
}
?
? ? ? // 編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點;
? ? ?function add(){
var num = prompt("請輸入學(xué)號","xh");
var name = prompt("請輸入姓名","xxx");
var newTr = document.createElement("tr");
?
var td1 = document.createElement("td");
td1.innerHTML = num;
?
var td2 = document.createElement("td");
td2.innerHTML = name;
?
var td3 = document.createElement('td');
var a = document.createElement("a");
a.setAttribute("href","javascript:;");
a.setAttribute("onclick","deleterow(this)");
a.innerHTML = "刪除";
td3.appendChild(a);
?
newTr.appendChild(td1);
newTr.appendChild(td2);
newTr.appendChild(td3);
?
var table = document.getElementById("table");
table.appendChild(newTr);
changeColor();
}
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function deleterow(obj){
obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
}
? </script>
<body>?
? ?<table border="1" width="50%" id="table">
? ?<tr>
<th>學(xué)號</th>
<th>姓名</th>
<th>操作</th>
? ?</tr>??
? ?<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onclick="deleterow(this)">刪除</a></td>? ?<!--在刪除按鈕上添加點擊事件? -->
? ?</tr>
? ?<tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:;" onclick="deleterow(this)">刪除</a></td>? ?<!--在刪除按鈕上添加點擊事件? -->
? ?</tr>??
? ?</table>
? ?<input type="button" value="添加一行" onclick="add()" />? ?<!--在添加按鈕上添加點擊事件? -->
</body>
</html>
2020-08-18
這個判斷為空不添加好像不太好用
2020-08-18
//這里加個判斷,為空的話不添加
var num = prompt("請輸入學(xué)號","xh");
var name = prompt("請輸入姓名","xxx");
if(!num && !name){
return;
}