js 给所有加载本JS页面地指定标签进行绑定事件

来源:互联网 发布:淘宝如何推爆款 编辑:程序博客网 时间:2024/04/30 08:15
 

 可能有时会遇到给页面地列表绑定移入移出事件,可是每个页面写又太麻烦所以就想到了,以下:

<table width="80%" height="80%" align="center" border="1">
<tr bordercolor="#CC3300">
<td style="cursor:pointer;">&nbsp;1</td>
<td style="cursor:pointer;">&nbsp;1</td>
<td style="cursor:pointer;">&nbsp;1</td>
</tr>
<tr bordercolor="#00FF66">
<td style="cursor:pointer;">&nbsp;2</td>
<td style="cursor:pointer;">&nbsp;2</td>
<td style="cursor:pointer;">&nbsp;2</td>
</tr>
<tr bordercolor="#00FF66">
<td style="cursor:pointer;">&nbsp;3</td>
<td style="cursor:pointer;">&nbsp;3</td>
<td style="cursor:pointer;">&nbsp;3</td>
</tr>
<tr bordercolor="#6633FF">
<td style="cursor:pointer;">&nbsp;4</td>
<td style="cursor:pointer;">&nbsp;4</td>
<td style="cursor:pointer;">&nbsp;4</td>
</tr>
</table>

<script type="text/javascript" >
 var tds=document.getElementsByTagName("tr");
 for(var i=0;i<tds.length;i++){
  tds[i].onmouseover=function(){
   this.style.color="red";
    };
    tds[i].onmouseout=function(){
   this.style.color="blue";
    };
  }
</script>

 

原创粉丝点击