為什么鼠標只能移動到最后一行才能改變顏色,移動到其他行都沒有變化?
<!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 tr=document.getElementsByTagName("tr");
? ? ? ? for (i=0;i<tr.length;i++) {
? ? ? ? ? ? var x=tr[i];
? ? ? ? ? ? x.onmouseover=function(){
? ? ? ? ? ? ? ? x.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? x.onmouseout=function(){
? ? ? ? ? ? ? ? x.style.backgroundColor="#fff";
? ? ? ? ? ? }
? ? ? ? }
}
? ? ?
? ? ? // 編寫一個函數,供添加按鈕調用,動態(tài)在表格的最后一行添加子節(jié)點;
? ? function add() {
? ? ? ? var td_1=document.createElement("td");
? ? ? ? var td_2=document.createElement("td");
? ? ? ? var td_3=document.createElement("td");
? ? ? ? td.innerHTML="";
? ? ? ? var tr=document.createElement("tr");
? ? ? ? tr.appendChild(td);
? ? ? ? var table=document.getElementsByTagName("table");
? ? ? ? table[0].appendChild(tr);
? ? }?
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數
? ? function remo(obj) {
? ? ? ?var a=document.getElementsByTagName("a");
? ? ? ?obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
? ? }?
? </script>?
?</head>?
?<body>?
? <table border="1" width="50%" id="table">
? <tr>
<th>學號</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
? <tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;" onclick="remo(this)">刪除</a></td> ? <!--在刪除按鈕上添加點擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:;" onclick="remo(this)">刪除</a></td> ? <!--在刪除按鈕上添加點擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" ?onclick="add()"/> ? <!--在添加按鈕上添加點擊事件 ?-->
?</body>
</html>
2017-09-23
?window.onload = function(){
var x;
? ? ? ? var tr=document.getElementsByTagName("tr");
? ? ? ? for (i=0;i<tr.length;i++) {
? ? ? ? ? ? var x=tr[i];
}
? ? ? ? ? ? x.onmouseover=function(){
? ? ? ? ? ? ? ? x.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? x.onmouseout=function(){
? ? ? ? ? ? ? ? x.style.backgroundColor="#fff";
? ? ? ? ? ? }
}
試試這樣行不,不行就只能把變色事件寫到另一個函數里面再引用了
2017-09-11
?var tr=document.getElementsByTagName("tr");
? ? ? ? for (i=0;i<tr.length;i++) {
? ? ? ? ? var x=tr[i];
? ? ? ? ? ?x.onmouseover=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? x.onmouseout=function(){
? ? ? ? ? ? ? ? this.style.backgroundColor="#fff";
? ? ? ? ? ? }
? ? ? ? }
改成這樣就對了,window.onload執(zhí)行后進入循環(huán),但是在你點上去的時候i的循環(huán)已經走完了x=tr[2] 這樣就只能最后一個可以執(zhí)行,你手動把i的大小改為i<2 就發(fā)現只有第二個可以換顏色。改成this之后就會指向前一個設置的對象
2017-09-06
你好,我按照你的思路,完善了下你的代碼,請作為參考。
<!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 tr = document.getElementsByTagName("tr");
? ? ? ? ? ? for (i = 1; i < tr.length; i++) {// ? 個人覺得, 首行標題不需要有選中狀態(tài),故從1開始,當然從0也行- -
? ? ? ? ? ? ? ? var x = tr[i];
? ? ? ? ? ? ? ? mouseInOut(x);
?? ? ? ? ? ?}
?? ? ? ?}
? ? ? ? // 編寫一個函數,供添加按鈕調用,動態(tài)在表格的最后一行添加子節(jié)點;
? ? ? ? function add() {
? ? ? ? ? ? var td_1 = document.createElement("td");
? ? ? ? ? ? var td_2 = document.createElement("td");
? ? ? ? ? ? var td_3 = document.createElement("td");
? ? ? ? ? ? td_1.innerHTML = "";
? ? ? ? ? ? td_2.innerHTML = "";
? ? ? ? ? ? td_3.innerHTML = '<a href="javascript:;" onclick="remo(this)">刪除</a>';
? ? ? ? ? ? var tr = document.createElement("tr");
????????????//下面是補充部分:
? ? ? ? ? ? tr.appendChild(td_1);
? ? ? ? ? ? tr.appendChild(td_2);
? ? ? ? ? ? tr.appendChild(td_3);
? ? ? ? ? ? mouseInOut(tr);
? ? ? ? ? ? var table = document.getElementsByTagName("table");
? ? ? ? ? ? table[0].appendChild(tr);
? ? ? ? }
? ? ? ? //公開一onmouseover、onmouseout的方法
? ? ? ? function mouseInOut(x) {
? ? ? ? ? ? x.onmouseover = function () {
? ? ? ? ? ? ? ? x.style.backgroundColor = "#f2f2f2";
? ? ? ? ? ? }
? ? ? ? ? ? x.onmouseout = function () {
? ? ? ? ? ? ? ? x.style.backgroundColor = "#fff";
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 創(chuàng)建刪除函數
? ? ? ? function remo(obj) {
? ? ? ? ? ? var a = document.getElementsByTagName("a");
? ? ? ? ? ? obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
? ? ? ? }?
? ? </script>
</head>
<body>
? ? <table border="1" width="50%" id="table">
? ? ? ? <tr>
? ? ? ? ? ? <th>
? ? ? ? ? ? ? ? 學號
? ? ? ? ? ? </th>
? ? ? ? ? ? <th>
? ? ? ? ? ? ? ? 姓名
? ? ? ? ? ? </th>
? ? ? ? ? ? <th>
? ? ? ? ? ? ? ? 操作
? ? ? ? ? ? </th>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? xh001
? ? ? ? ? ? </td>
? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? 王小明
? ? ? ? ? ? </td>
? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? <a href="javascript:;" onclick="remo(this)">刪除</a>
? ? ? ? ? ? </td>
? ? ? ? ? ? <!--在刪除按鈕上添加點擊事件 ?-->
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? xh002
? ? ? ? ? ? </td>
? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? 劉小芳
? ? ? ? ? ? </td>
? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? <a href="javascript:;" onclick="remo(this)">刪除</a>
? ? ? ? ? ? </td>
? ? ? ? ? ? <!--在刪除按鈕上添加點擊事件 ?-->
? ? ? ? </tr>
? ? </table>
? ? <input type="button" value="添加一行" onclick="add()" />
? ? <!--在添加按鈕上添加點擊事件 ?-->
</body>
</html>