标题 | javascript表格隔行变色加鼠标移入移出及点击效果的方法 |
内容 | 表格隔行变色,也是一个提高用户体验的js效果。 效果实现: 表格奇偶行的颜色不一样。这样可以防止用户看数据时串行。 鼠标移入某行时变颜色,移出再变回来。这样可以让用户清楚的知道自己正在看哪一行。 表格点击变色。便于用户选中自己想保留的项。 说明: i%2 每个数和2取模的值,只有0和1两种,这样就可以实现隔行变色的效果 tables_li[i].onoff = 1; 为了实现点击变色,鼠标移入移出时,颜色不被覆盖。 上代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title>无标题文档</title> <style> table{ border-collapse:collapse } table td{ height:26px; font-size:12px; color:#333; border:1px solid #09c; text-align:center; padding:0 10px; } </style> </head> <body> <script> window.onload = function(){ var tables = document.getElementById("tables"); var tables_li = tables.getElementsByTagName("tr"); var i=0; var len = tables_li.length; for(i=0; i<len; i++){ tables_li[i].onoff = 1; tables_li[i].index = i; tables_li[i].style.backgroundColor = i%2?"#ace":""; tables_li[i].onmouseover = function(){ if(this.onoff == 1){ this.style.backgroundColor = "#06c"; } } tables_li[i].onmouseout = function(){ if(this.onoff == 1){ this.style.backgroundColor = this.index%2?"#ace":""; } } tables_li[i].onclick = function(){ if(this.onoff == 1){ this.onoff = 0; this.style.backgroundColor = "#069"; }else{ this.onoff = 1; this.style.backgroundColor = this.index%2?"#ace":""; } } } } </script> <table width="500" cellpadding="0" cellspacing="0" id="tables"> <tr> <td>1</td> <td>内容内容</td> </tr> <tr> <td>2</td> <td>内容内容</td> </tr> <tr> <td>3</td> <td>内容内容</td> </tr> <tr> <td>4</td> <td>内容内容</td> </tr> <tr> <td>5</td> <td>内容内容</td> </tr> <tr> <td>6</td> <td>内容内容</td> </tr> <tr> <td>7</td> <td>内容内容</td> </tr> <tr> <td>8</td> <td>内容内容</td> </tr> <tr> <td>9</td> <td>内容内容</td> </tr> <tr> <td>10</td> <td>内容内容</td> </tr> </table> </body> </html> |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。