表格的鼠标事件以及JavaScript动态修改DOM

来源:互联网 发布:朱苏力 严重问题知乎 编辑:程序博客网 时间:2024/06/11 12:48

我一直想要鼠标对表格的行或者单元格起反应,结果才疏学浅,总不得法。近来在网上看到一篇源码,颇有感触,小试牛刀,还望不要见笑。另外,进来看Ajax的书,经常有通过JS动态修改DOM改变页面的方法,就乘机与表格的鼠标事件连在一起,弄了点东西出来,以后也可作为参考。代码如下,附有简单注释:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>测试表格行事件</title>
<script>
function test(element){
 //改变元素的背景色
 document.getElementById(element.id).style.backgroundColor = "#FFAAFF";
 //获得tr元素,如果已经存在事件列之外的列,删除之
 var tr = document.getElementById("t")
 for (var i=tr.childNodes.length-1; i>0; i--){
  tr.removeChild(tr.childNodes[i]);
 }
 var cell = document.createElement("td");
 cell.setAttribute("bgcolor","#FF0000");//这两个setAttribute都没有起作用
 cell.setAttribute("border","0");
 cell.style.backgroundColor = "#FFFFFF";//实际起作用的是这个
 var text = document.createTextNode("使用JS创建TD");
 cell.appendChild(text);
 tr.appendChild(cell);
}
</script>
</head>

<body>
<!--使用table的背景色与TR的背景色不同,可以制造出solid的线条,但是使用cellspacing="1"制造的线条更细一点-->
<table border="0" cellpadding="1" cellspacing="1" bgcolor="#FF0000">
 <tr id="t">
 <td bgcolor="#FFFFFF" id="a" onmouseover="javascript:test(this);">测试鼠标划过</td>
 </tr>
 <tr bgcolor="#FFFFFF"><td>没有事件</td></tr>
</table>
</body>
</html>
 这其中我还引入了得到较细的表格边框线条的方法,以前一直想得到这种效果,终于找到方法了。如果想要将外围的边框弄粗一点,可以在外边套个表格。

这里的表格鼠标事件极其简单,如果想要更加复杂一点的,我将网上的那篇源码贴在后边,虽然不知道作者是谁,仍然很感激。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>mytable</title>
<script language="JavaScript">
 
 function anole(
 str_tableid, // table id
  num_header_offset,// 表头行数
  str_odd_color, // 奇数行的颜色
  str_even_color,// 偶数行的颜色
  str_mover_color, // 鼠标经过行的颜色
  str_onclick_color // 选中行的颜色
  )  {

 // 表格ID参数验证
  if(!str_tableid) return alert(str_tableid+"表格不存在");
 var obj_tables=(document.all ? document.all[str_tableid]:document.getElementById(str_tableid));
 if(!obj_tables) return alert("ID为("+str_tableid+")不存在!");

 // 设置个参数的缺省值
  var col_config=[];
 col_config.header_offset=(num_header_offset?num_header_offset:0 );
 col_config.odd_color=(str_odd_color?str_odd_color:'#ffffff');
 col_config.even_color=(str_even_color?str_even_color:'#dbeaf5');
 col_config.mover_color=(str_mover_color?str_mover_color:'#6699cc');
 col_config.onclick_color=(str_onclick_color?str_onclick_color:'#4C7DAB');
 // 初始化表格(可能多个表格用同一个ID)
  if(obj_tables.length)
  for(var i=0;i<obj_tables.length;i++ )
 tt_init_table(obj_tables[i],col_config);
 else
 tt_init_table(obj_tables,col_config);
}
 
 function tt_init_table(obj_table,col_config)  {
 var col_lconfig=[],
 col_trs=obj_table.rows;
 if(!col_trs) return ;
 
 for(var i=col_config.header_offset;i<col_trs.length;i++)  { // i 从 表头以下开始
  col_trs[i].config=col_config;
 col_trs[i].lconfig=col_lconfig;
 col_trs[i].set_color=tt_set_color;
 col_trs[i].onmouseover=tt_mover;
 col_trs[i].onmouseout=tt_mout;
 col_trs[i].onmousedown=tt_onclick;
 col_trs[i].order=(i-col_config.header_offset)%2 ;
 col_trs[i].onmouseout();
 }
}
 function tt_set_color(str_color) {
 this.style.backgroundColor=str_color;
}
 
 // 事件操作
 function tt_mover() {
 if(this.lconfig.clicked!=this )
  this.set_color(this.config.mover_color);
}
 function tt_mout() {
 if(this.lconfig.clicked!=this )
  this.set_color(this.order?this.config.odd_color:this.config.even_color);
}
 function tt_onclick()  {
 if( this.lconfig.clicked==this) {
  this.lconfig.clicked=null;
  this.onmouseover();
 }
  else {
  var last_clicked=this.lconfig.clicked;
  this.lconfig.clicked=this ;
  if(last_clicked) last_clicked.onmouseout();
  this.set_color(this.config.onclick_color);
 }
}
 
</script>
</head>

<body>
<table bgcolor="#FF0000" align="center" cellpadding="1" cellspacing="0" width="80%">
 <tr>
 <td>
 <table id="demo" cellpadding="1" cellspacing="1" border="0" width="100%" align="center">
 <tr><th colspan="2" bgcolor="ffffff">HTML document object properties</th></tr>
 <tr><td width="20%">activeElement</td><td>Retrieves the object that has the focus.</td></tr>
 <tr><td>aLinkColor</td><td>Sets or retrieves the color of all links in the document.</td></tr>
 <tr><td>bgColor</td><td>Sets or retrieves the background color behe document object.</td></tr>
 <tr><td>body</td><td>Specifies the beginning and end of the document body.</td></tr>
 <tr><td>contentEditable</td><td>Sets or retrieves whether the userdocument object.</td></tr>
 <tr><td>cookie</td><td>Sets or retrieves the string value of a cookie.</td></tr>
 <tr><td>defaultCharset</td><td>Sets or retrieves the default chara of the document.</td></tr>
 <tr><td>designMode</td><td>Sets or retrieves whether the document can be edited.</td></tr>
 <tr><td>documentElement</td><td>Retrieves a reference to the root node of the document.</td></tr>
 <tr><td>domain</td><td>Sets or retrieves the security domain of the document.</td></tr>
 </table>
 </td>
 </tr>
 </table>
 <script language="JavaScript">
  anole('demo',1,'#ffffff','#ccccff','#ffccff','#cc99ff');
 </script>
</body>
</html>