可以幫我看看添加哪里出錯(cuò)了
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ??
? <script type="text/javascript">?
??
? ? ? window.onload = function(){
? ? ? ? var trLength=document.getElementsByTagName("tr");
? ? ? ? for(var i=0;i<trLength.length;i++){
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過(guò)給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景色。
? ? ? ? ? ? changebg(trLength[i]);
? ? ? ? ? ??
? ? ? ? }?
function changebg(obj){
? ?obj.onmouseover=function(){
? ? ? ?obj.style.backgroundColor="#f2f2f2";
? ?}?
? ?obj.onmouseout=function(){
? ? ? ?obj.style.backgroundColor="#fff";
? ?}?
}
? ? ?
?
}
? ? ? ??
? ? ? // 編寫(xiě)一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ? ? function add(){
? ? ? ? ? ? var t=document.getElementsByTagName("tr");
? ? ? ? ? ? ?var num=t.length;
? ? ? ? ? ? var a=document.createElement("tr");
? ? ? ? ? ? var b=document.createElement("td");
? ? ? ? ? ? var c=document.createElement("td");
? ? ? ? ? ? var d=document.createElement("td");
? ? ? ? ? ? b.innerHTML="xh00"+num+"</td>";
? ? ? ? ? ? c.innerHTML="第"+num+"個(gè)學(xué)生"+"</td>";
? ? ? ? ? ? d.innerHTML='<a href="javascript:;" ?onclick="c(this)">刪除</a>'+"</td>"+"</tr>";
? ? ? ? ? ? var top=document.getElementsByTagName("table");
? ? ? ? ? ? top.appendChild(a);
? ? ? ? ? ? a.appendChild(b);
? ? ? ? ? ? a.appendChild(c);
? ? ? ? ? ? a.appendChild(d);
? ? ? ? ? ? ?for(var i=0;i<num;i++){
? ? ? ? ? ? ? ? changebg(t[i]);
? ? ? ? ? ? }?
? ? ? ? }
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function c(deletestu){
? ? ? ?
? ? ? ? var ?stu=deletestu.parentNode.parentNode;
? ? ? ? stu.parentNode.removeChild(stu);
? ? ?}
? </script>?
?</head>?
?<body>?
? <table border="1" width="50%" id="table">
? <tr>
<th>學(xué)號(hào)</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
? <tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" ?onclick="c(this)">刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:;" onclick="c(this)">刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" onclick="add()" /> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
2017-09-23
你的代碼問(wèn)題是大括號(hào)}錯(cuò)誤,第一個(gè)函數(shù)?window.onload = function()的結(jié)尾大括號(hào)}跑到第二個(gè)函數(shù)function changebg(obj)后面了
2017-09-23
問(wèn)一下,
??// 創(chuàng)建刪除函數(shù)
??function c(deletestu){
? ? ? ?var ?stu=deletestu.parentNode.parentNode;
? ? ? ? stu.parentNode.removeChild(stu);
? ? ?}
這個(gè)函數(shù)能解釋下嗎,為什么這么寫(xiě),
onclick="c(this)"為什么調(diào)用的時(shí)候里面是this,什么意思