為什么我添加的行,不會(huì)隨鼠標(biāo)的經(jīng)過(guò)而改變顏色呢?
代碼如下:
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ??
? <script type="text/javascript">?
??
? ? ? window.onload = function(){
? ? ? var mychar= document.getElementsByTagName("tr");
? ? ? for(i=0;i<mychar.length;i++)
? ? ? {
? ? ? changeColor(mychar[i]);
? ? ? }
? ? ? } ? ? ? ? ? ?
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過(guò)給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來(lái)改變所在行背景色。
? ? ? function changeColor(mycolor){ ??
? ? ? ? ?mycolor.onmouseover = function(){
? ? ? ? ? mycolor.style.backgroundColor = "blue";
? ? ? ? ?}
? ? ? ? ?mycolor.onmouseout = function(){
? ? ? ? ? mycolor.style.backgroundColor = "white";
? ? ? ? ?}
? ? ?
?
?
? ? ?}
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ?var num = 2;
? ? ?function addtr(){
? ? ? num++;
? ? ? var ntr = document.createElement("tr");
? ? ? var ntd1 = document.createElement("td");
? ? ? var ntd2 = document.createElement("td");
? ? ? if(num<10)
? ? ? {
? ? ? ntd1.innerHTML = "xh00"+num;
? ? ? }else if(num>=10&&num<100)
? ? ? {
? ? ? ntd1.innerHTML = "xh0"+num;
? ? ? }else
? ? ? {
? ? ? ntd1.innerHTML = "xh"+num;
? ? ? }
? ? ? ntd2.innerHTML = "第"+num+"位學(xué)生";
? ? ? var del = document.createElement("td");
? ? ? del.innerHTML = "<a href='javascript:;' onclick='del(this)'>刪除</a>";
? ? ? var tab = document.getElementById("table");
? ? ? tab.appendChild(ntr);
? ? ? ntr.appendChild(ntd1);
? ? ? ntr.appendChild(ntd2);
? ? ? ntr.appendChild(del);
? ? ?}
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? function del(obj){
? ? var tr = obj.parentNode.parentNode;
? ? tr.parentNode.removeChild(tr);
? ? }
? </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="addtr()"/> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
2016-04-12
改變HTML元素樣式的語(yǔ)法是
Object.style.property= new style; ? ? 如:Object.style.backgroundColor = "white";
Object是獲取的元素對(duì)象,例如通過(guò)document.getElementById("id");
mycolor是參數(shù)而非對(duì)象
2016-04-15
樓主,我試了試你的代碼,可以???根本沒(méi)啥問(wèn)題????
2016-04-12
非常感謝各位的解答
2016-04-12
mycolor是參數(shù),不是對(duì)象