如何在OnClick事件中设置,当点击表格中的某行时本行改变颜色?

sspanzervor 2006-02-17 05:14:55
部分代码如下:
......
<tr id="tr_main" onClick="if (this.style.background='#FFFFFF') this.style.background='#CCCCCC';else this.style.background='#FFFFFF';">
。。。
想实现如下的效果:如果点击之前行的背景色为白色,则变为灰色;如果为灰色则变为白色;但是在实际执行中,当点击白色行时可以变为灰色;但是点击灰色行时,却无法回复背景色(白色)请问这是怎么回事?应该如何修改?谢谢!
...全文
505 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sspanzervor 2006-02-24
  • 打赏
  • 举报
回复
大小写!在JS中即使常量也要注意大小写,有点失望...... 删贴给分。
zxy945 2006-02-20
  • 打赏
  • 举报
回复
<table>
<tr id="0" onclick="if ((this.style.background=='')||(this.style.background=='#ffffff')) {this.style.background='#CCCCCC'; }
else {this.style.background='#ffffff';}">
<td>111</td><td>222</td>
</tr>
</table>
DeluxWorld 2006-02-20
  • 打赏
  • 举报
回复
<table>
<tr id="0" onclick="if (this.style.background=='') {this.style.background='#CCCCCC'; alert(this.style.background);}
else {this.style.background='#FFFFFF';alert(this.style.background);}">
<td>111</td><td>222</td>
</tr>
</table>
sspanzervor 2006-02-20
  • 打赏
  • 举报
回复
哦谢谢,但是为什么对于某一行来说,我点击了2次之后,再次点击就没有相应了?代码如下:
<tr id="0" onclick="if (this.style.background=='') {this.style.background='#CCCCCC'; alert(this.style.background.value);}
else {this.style.background='#FFFFFF';alert(this.style.background.value);}">

另外,为什么alert中的值是“undefined”?如果要返回当前行的背景值,那么我应该如何写?谢谢!
任亚军 2006-02-20
  • 打赏
  • 举报
回复

得用双等号
lishery 2006-02-20
  • 打赏
  • 举报
回复

<tr id="tr_main" onClick="JavaScript:if (this.style.background=='#FFFFFF') this.style.background='#CCCCCC';else this.style.background='#FFFFFF';">
DeluxWorld 2006-02-20
  • 打赏
  • 举报
回复
if (this.style.background=='#FFFFFF') 中,if条件不成立,进入不到函数体
改为if (this.style.background=='')试试
sspanzervor 2006-02-20
  • 打赏
  • 举报
回复
......
<tr id="tr_main" onClick="if (this.style.background=='#FFFFFF') this.style.background='#CCCCCC';else this.style.background='#FFFFFF';">
。。。
但是我已经更改成为“==”了,为什么现在程序没有相应了?点击也某行没有任何反应?请各位高手多多指教!
Hellohuan 2006-02-20
  • 打赏
  • 举报
回复
+up +jf

--------------------
我用ASP写的技术社区:
http://udclub.com/cn/community/default.html
linky520 2006-02-18
  • 打赏
  • 举报
回复
<script>

var idcolor = null;
function trcolor(id,sourcol)
{

var obj = document.getElementById(id);

if(obj == null) return;

obj.bgColor = "#B0D8FF";

var oldobj = document.getElementById(idcolor);

idcolor = id;
if(oldobj == null) return; oldobj.bgColor = ""+sourcol+""; }

</script>
<table>
<tr id="0" onclick="javascript:trcolor(this.id,'#FF0000');" bgcolor="#FF0000">
<td height="20">1</td>
</tr>
<tr id="1" onclick="javascript:trcolor(this.id,'#DF0000');" bgcolor="#DF0000">
<td height="20">1</td>
</tr>
</table>
超级大笨狼 2006-02-17
  • 打赏
  • 举报
回复
==
ShiningstarHu 2006-02-17
  • 打赏
  • 举报
回复
你有一个逻辑上的bug
onClick="if (this.style.background='#FFFFFF') 中

this.style.background='#FFFFFF' //这个表示付值! 结果永远返回true !

判断因该是 this.style.background=='#FFFFFF'

这是写 C/C++ Java 中容易写错的地方!:D

搂主要小心啊:D
chouchy 2006-02-17
  • 打赏
  • 举报
回复
<table border="1">
<tr onclick="this.style.backgroundColor=(this.style.backgroundColor=='')?'#CCC':''">
<td width=100>111</td>
<td width=100>222</td>
</tr>
</table>
DeluxWorld 2006-02-17
  • 打赏
  • 举报
回复
可以这样:
<tr onclick="JavaScript:if(this.bgColor == '')this.bgColor='green';else{this.bgColor = ''}">
sspanzervor 2006-02-17
  • 打赏
  • 举报
回复
前辈,小弟才疏学浅,还请多多指教!
whb147 2006-02-17
  • 打赏
  • 举报
回复
你用alert(this.style.background)
看看就知道了

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧