<!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)移動改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
?????????var?tabletr=document.getElementsByTagName("tr");
?????????for(var?i=0;i<tabletr.length;i++)
?????????{
?????????????tabletr[i].onmouseover=function()
?????????????{
?????????????????tabletr[i].style.backgroundColor="red";
?????????????}
?????????????tabletr[i].onmouseout=function()
?????????????{
?????????????????tabletr[i].style.backgroundColor="#fff";
?????????????}
?????????}
?????}
?
??</script>?
?</head>?
?<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:;"?>刪除</a></td>???
???????</tr>
???????<tr>
????????<td>xh002</td>
????????<td>劉小芳</td>
????????<td><a?href="javascript:;"?>刪除</a></td>??
???????</tr>??
???????</table>
???????<input?type="button"?value="添加一行"??/>??
?</body>
</html>以上為代碼部分,但是在寫這段代碼的時候,無法得出預(yù)期結(jié)果,即鼠標(biāo)經(jīng)過的時候不觸發(fā)事件。后來看到別人的代碼,發(fā)現(xiàn)將tabletr[i].style.backgroundColor改為this.style.backgroundColor后就成功了。我想知道為什么在這里使用tabletr[i]不行而this則可以呢?還有這里的this代表的什么,難道不是代表的tabletr[i]嗎?
為什么這里使用tabletr[i]不行而this則可以呢?
老夫不是寵物
2017-03-01 00:30:38