1鼠標(biāo)停留劃過都沒有看到有顏色變化;2 onload=function()為什么沒有函數(shù)名? 3原有兩行點(diǎn)了刪除無反應(yīng),新添一行刪除卻有反應(yīng)
<!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 (){
? ? ? ? ? ? ? ? ??
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
? ? ? ? var tr=document.getElementByTagName("tr");
? ? ? ? for(var i=0;i<tr.length;i++)
? ? ? ? bgcChange(tr[i]);
}
function bgcChange(obj)
? ? ?{
? ? ? ? ?obj.onmouseover=function(){
? ? ? ? ? ? ?obj.style.backgroundColor="#f2f2f2";
? ? ? ? ?}
? ? ? ? ?obj.onmouseout=function(){
? ? ? ? ? ? ?obj.style.backgroundColor="#fff";
? ? ? ? ?}
? ? ?}
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ?function add()
? ? ?{
? ? ? ? ?var xh=prompt("請(qǐng)輸入學(xué)號(hào)");
? ? ? ? ?var xm=prompt("請(qǐng)輸入姓名");
? ? ? ? ?var table=document.getElementById("table");
? ? ? ? ?var newtr=document.createElement("tr");
? ? ? ? ?var td1=document.createElement("td");
? ? ? ? ?td1.innerHTML=xh;
? ? ? ? ?var td2=document.createElement("td");
? ? ? ? ?td2.innerHTML=xm;
? ? ? ? ?var td3=document.createElement("td");
? ? ? ? ?td3.innerHTML="<a href='JavaScript:;' onclick='del(this)'>刪除</a>"
? ? ? ? ?table.appendChild(newtr);
? ? ? ? ?newtr.appendChild(td1);
? ? ? ? ?newtr.appendChild(td2);
? ? ? ? ?newtr.appendChild(td3);
? ? ? ? ?var tr=document.getElementByTagName("tr");
? ? ? ? ?for(var i=0;i<tr.length;i++)
? ? ? ? bgcChange(tr[i]);
? ? ?}
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function del(obj)
? ? ?{
? ? ? ? ?var x=obj.parentNode.parentNode;
? ? ? ? ?document.getElementById("table").removeChild(x);
? ? ?}
? </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="del(this)" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:;" onclick="del(this)">刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" onclick="add()"/> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
2018-08-13
我測試的時(shí)候發(fā)現(xiàn)document.getElementById("table") 這個(gè)找到的是table沒錯(cuò),但是table下面其實(shí)還有一層"tbody",所有的tr 都在tbody下
2018-08-08
? var tr=document.getElementByTagName("tr"); 這句錯(cuò)了,改成getElementmentsByTagName("tr");
? this是js的一個(gè)關(guān)鍵字,隨著函數(shù)使用場合不同,this的值會(huì)發(fā)生變化。但是總有一個(gè)原則,那就是this指的是調(diào)用函數(shù)的那個(gè)對(duì)象。這里應(yīng)該指的<a>元素。
2018-08-08
del(this)這里的this不懂是什么意思 求詳解