[javaScript]改变鼠标经过表格行背景色
2016-01-16 14:24
701 查看
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script> window.onload=function(){ changeColor(); } function addElement(){ var tab=document.getElementById("table"); var new_id=document.createElement("td"); new_id.innerHTML="xh000"; var new_name=document.createElement("td"); new_name.innerHTML="徐小胖"; var new_ope=document.createElement("td"); new_ope.innerHTML="<a href='#' onclick='delElement(this)'>删除</a>"; var new_tr=document.createElement("tr"); new_tr.appendChild(new_id); new_tr.appendChild(new_name); new_tr.appendChild(new_ope); tab.lastChild.appendChild(new_tr); changeColor(); } function delElement(obj){ document.getElementById("table").lastChild.removeChild(obj.parentNode.parentNode); } function changeColor(){ var this_tr=document.getElementsByTagName("tr"); for(var i=0;i<this_tr.length;i++){ this_tr[i].onmouseover=function(){ this.style.backgroundColor="#f2f2f2"; } this_tr[i].onmouseout=function(){ this.style.backgroundColor="#fff"; } } } </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="delElement(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:;" onclick="delElement(this)">删除</a></td> <!--在删除按钮上添加点击事件 --> </tr> </table> <input type="button" value="添加一行" onclick="addElement()"/> <!--在添加按钮上添加点击事件 --> </body> </html>
相关文章推荐
- CreateJS-EaseJS 应用
- JavaScript 教程
- JavaScript摘要
- JavaScript面向对象精要
- javascript学习笔记(一)
- JavaScript 学习摘要(2)
- AnjularJs的应用
- JavaScript中的方法重载
- js 获取地址栏参数
- 读取本地Json文件
- 本地 json文件转化成plist文件
- JS-JavaScript学习笔记(一)
- 解决Eclipse报errors running builder ‘javascript validator’ on project
- classList详解,让你的js方便地操作DOM类
- JSON格式解析和libjson使用简介-转
- cJSON库介绍
- 三款简单的JS返回页面顶部代码
- js-事件
- eclipse中默认HTML和js编辑器非常慢,尤其在拷贝粘贴代码时的解决方法
- js study