当我点击某一行的时候,该行的背景色变成我想要的颜色
当我点击另外一行时,该行变成我想要的颜色,同时之前的背景色还原成白色
也就是说,不管我怎么点击,只有最后一次点击的行的背景色是我所想要的颜色
9 个解决方案
#1
修改 td 的bgcolor
#2
行是 tr
#3
事点击还是鼠标划过???
点击事onclick 划过onmouseover
点击事onclick 划过onmouseover
#4
<style type="text/css">
<!--
tr{background-color:expression((this.sectionRowIndex%2==0)?'#f7f7f7':'#ffffff')}
-->
</style>
<table width="768" border="0" align="center" cellpadding="0" cellspacing="0">
<%for i=0 to 50%>
<tr>
<td height="20" align="center"> </td>
</tr>
<%next%>
</table>
<!--
tr{background-color:expression((this.sectionRowIndex%2==0)?'#f7f7f7':'#ffffff')}
-->
</style>
<table width="768" border="0" align="center" cellpadding="0" cellspacing="0">
<%for i=0 to 50%>
<tr>
<td height="20" align="center"> </td>
</tr>
<%next%>
</table>
#5
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鼠标经过表格行背景变色-80端口</title>
</head>
<body>
<table width="55%" border="2" align="center">
<tr onmouseover="this.style.backgroundColor='#cc0000'" onmouseout="this.style.backgroundColor=''">
<td><div align="center">表格内容</div></td>
<td><div align="center">表格内容</div></td>
</tr>
<tr onmouseover="this.style.backgroundColor='#cc0000'" onmouseout="this.style.backgroundColor=''">
<td><div align="center">表格内容</div></td>
<td><div align="center">表格内容</div></td>
</tr>
<tr onmouseover="this.style.backgroundColor='#cc0000'" onmouseout="this.style.backgroundColor=''">
<td><div align="center">表格内容</div></td>
<td><div align="center">表格内容</div></td>
</tr>
</table>
</body>
</html>
#6
表格隔行变色,鼠标放上变不同颜色。兼容IE,FF firefox :
<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,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.config=col_config;
col_trs.lconfig=col_lconfig;
col_trs.set_color=tt_set_color;
col_trs.onmouseover=tt_mover;
col_trs.onmouseout=tt_mout;
col_trs.onmousedown=tt_onclick;
col_trs.order=(i-col_config.header_offset)%2 ;
col_trs.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="#9933ff" 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>
#7
<SCRIPT language=JavaScript>
function Cbg(obj, objColor)
{
obj.style.backgroundColor=objColor
}
</SCRIPT>
<table>
<tr onmouseover="Cbg(this, '66ff99')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
<tr onmouseover="Cbg(this, 'ff9966')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
</table>
function Cbg(obj, objColor)
{
obj.style.backgroundColor=objColor
}
</SCRIPT>
<table>
<tr onmouseover="Cbg(this, '66ff99')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
<tr onmouseover="Cbg(this, 'ff9966')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
</table>
#8
<tr class="trh1" onmouseout="this.style.backgroundColor='#FFFDE8'" onclick="this.style.backgroundColor='#ffffff'" style="background-color: #FFFDE8">
<td>
</td></tr>
#9
<tr class="trh1" onmouseout="this.style.backgroundColor='#FFFDE8'" onclick="this.style.backgroundColor='#ffffff'" style="background-color: #FFFDE8">
<td>
</td></tr>
#1
修改 td 的bgcolor
#2
行是 tr
#3
事点击还是鼠标划过???
点击事onclick 划过onmouseover
点击事onclick 划过onmouseover
#4
<style type="text/css">
<!--
tr{background-color:expression((this.sectionRowIndex%2==0)?'#f7f7f7':'#ffffff')}
-->
</style>
<table width="768" border="0" align="center" cellpadding="0" cellspacing="0">
<%for i=0 to 50%>
<tr>
<td height="20" align="center"> </td>
</tr>
<%next%>
</table>
<!--
tr{background-color:expression((this.sectionRowIndex%2==0)?'#f7f7f7':'#ffffff')}
-->
</style>
<table width="768" border="0" align="center" cellpadding="0" cellspacing="0">
<%for i=0 to 50%>
<tr>
<td height="20" align="center"> </td>
</tr>
<%next%>
</table>
#5
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鼠标经过表格行背景变色-80端口</title>
</head>
<body>
<table width="55%" border="2" align="center">
<tr onmouseover="this.style.backgroundColor='#cc0000'" onmouseout="this.style.backgroundColor=''">
<td><div align="center">表格内容</div></td>
<td><div align="center">表格内容</div></td>
</tr>
<tr onmouseover="this.style.backgroundColor='#cc0000'" onmouseout="this.style.backgroundColor=''">
<td><div align="center">表格内容</div></td>
<td><div align="center">表格内容</div></td>
</tr>
<tr onmouseover="this.style.backgroundColor='#cc0000'" onmouseout="this.style.backgroundColor=''">
<td><div align="center">表格内容</div></td>
<td><div align="center">表格内容</div></td>
</tr>
</table>
</body>
</html>
#6
表格隔行变色,鼠标放上变不同颜色。兼容IE,FF firefox :
<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,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.config=col_config;
col_trs.lconfig=col_lconfig;
col_trs.set_color=tt_set_color;
col_trs.onmouseover=tt_mover;
col_trs.onmouseout=tt_mout;
col_trs.onmousedown=tt_onclick;
col_trs.order=(i-col_config.header_offset)%2 ;
col_trs.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="#9933ff" 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>
#7
<SCRIPT language=JavaScript>
function Cbg(obj, objColor)
{
obj.style.backgroundColor=objColor
}
</SCRIPT>
<table>
<tr onmouseover="Cbg(this, '66ff99')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
<tr onmouseover="Cbg(this, 'ff9966')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
</table>
function Cbg(obj, objColor)
{
obj.style.backgroundColor=objColor
}
</SCRIPT>
<table>
<tr onmouseover="Cbg(this, '66ff99')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
<tr onmouseover="Cbg(this, 'ff9966')" onmouseout="Cbg(this, 'ffffff')">
<td height="20">111</td>
<td height="20">111</td>
</tr>
</table>
#8
<tr class="trh1" onmouseout="this.style.backgroundColor='#FFFDE8'" onclick="this.style.backgroundColor='#ffffff'" style="background-color: #FFFDE8">
<td>
</td></tr>
#9
<tr class="trh1" onmouseout="this.style.backgroundColor='#FFFDE8'" onclick="this.style.backgroundColor='#ffffff'" style="background-color: #FFFDE8">
<td>
</td></tr>