try something like
<script language="javascript">
var tr=null;function setColor()
{
if (tr != null)
{
tr.style.backgroundColor="";
tr = null;
}
var e = event.srcElement;
e = e.parentElement;
while (e != null && e.tagName != "TR")
e = e.parentElement; if (e != null && e.tagName == "TR")
tr = e; if (tr != null)
tr.style.backgroundColor="red";
}
</script>
<table>
<tbody>
<tr onclick="setColor()">
<td>hello</td><td>hello</td><td>hello</td>
</tr>
<tr onclick="setColor()">
<td>hello</td><td>hello</td><td>hello</td>
</tr>
<tr onclick="setColor()">
<td>hello</td><td><font size="12"><div>hello<div></font></td><td>hello</td>
</tr>
</tbody>
</table>
<script language="javascript">
var tr=null;function setColor()
{
if (tr != null)
{
tr.style.backgroundColor="";
tr = null;
}
var e = event.srcElement;
e = e.parentElement;
while (e != null && e.tagName != "TR")
e = e.parentElement; if (e != null && e.tagName == "TR")
tr = e; if (tr != null)
tr.style.backgroundColor="red";
}
</script>
<table>
<tbody>
<tr onclick="setColor()">
<td>hello</td><td>hello</td><td>hello</td>
</tr>
<tr onclick="setColor()">
<td>hello</td><td>hello</td><td>hello</td>
</tr>
<tr onclick="setColor()">
<td>hello</td><td><font size="12"><div>hello<div></font></td><td>hello</td>
</tr>
</tbody>
</table>
解决方案 »
- 高手们,速度啊,在线等。。。急急急。。。
- swfupload在FF下无法取到cookie值
- 急!!这个正则怎么写?
- 一个IE兼容的问题。
- jQuery 模仿51job的首页的 选择工作地址~~~急求
- 按回车提交表单,但又不提交表单的问题!
- 高分求可以在框架网页下使用的Javascript 日历代码
- 导航栏问题
- 如何根据image控件的visible属性来判断是否打印本页?
- 如何让文本框输入数字和小数点,还有减号!谢谢
- ****************我郁闷!!!进者有分!!!另外请孟子E章 梅花rain 思归等人解答!!********************
- 求助!支持中文的字形生成器(vbscript或javascript)
{
if(e.tagName=="TR") return e
e=e.parentElement
if(e.tagName=="BODY") return null
return getTR(e)
}
.....
lastClickObject=getTR(event.srcElement)....
如果你在<font>中定义了颜色/背景色,那么tr的颜色/背景色无法覆盖<font>中的设置
<head>
</head><xml id=testTable>
<root>
<tuple>
<firstname>Dickensi </firstname>
<lastname>Wen</lastname>
</tuple>
<tuple>
<firstname>Steve</firstname>
<lastname>Tao</lastname>
</tuple>
<tuple>
<firstname>dfdsfsd</firstname>
<lastname>dfsf</lastname>
</tuple>
</root>
</xml><script>
// load ur table from the xml
var oOldCurTr = null;
var bgColor = {select:"#cc7d8d",normal:"#ffffff"};function onLoadPage(){
var oNodes = testTable.XMLDocument.selectNodes(".//tuple");
var strInnerHTML = "<table cellpadding='0' cellspacing='0' onclick='onDKSMouseClick()'><TBODY>" ;
for(var i=0; i<oNodes.length; i++){
strInnerHTML += "<tr><td>";
strInnerHTML += oNodes[i].selectSingleNode(".//firstname").text + "  ";
strInnerHTML += "</td><td>"
strInnerHTML += oNodes[i].selectSingleNode(".//lastname").text;
strInnerHTML += "</td></tr>";
}
strInnerHTML += "</TBODY></table>" ;
myTableContainer.innerHTML = strInnerHTML;}function onDKSMouseClick(){
var oEle = event.srcElement;
var oTr = getCurrentTr(oEle);
if(oTr != null){
if(oOldCurTr != null){
oOldCurTr.bgColor = bgColor.normal;
}
oOldCurTr = oTr ;
oTr.bgColor = bgColor.select;
}
}function getCurrentTr(oEle){
var oTrItem = oEle;
while(oTrItem.tagName.toUpperCase() != "TR"){
oTrItem = oTrItem.parentElement;
if(oTrItem == null){
break;
}
}
return oTrItem;
}
</script><body onload="onLoadPage()"><table>
<tr><td id=myTableContainer>dfdf</td></tr>
</table></body>
</html>
未经测试,可能有bug
事情出在冒泡机制上。你用连续的两个parentNode并不能解决问题。
即使你将事件处理器设定在TR一级,事件仍然是由最底层或者说最重孙辈的娃娃们触发,然后向上传直到碰到处理器。
因为触发事件的元素并不一定每次都在同一个层次。每一个可以接收事件的元素都可能是SrcElement,这取决于你究竟按在哪里了。例如,如果TD里有个元素<br>,这个BR都有可能触发事件;如果TD内完全是空的,事件由TD触发。我打赌你一定想在附加事件的时候加上个this参数,可惜加不上:-)))
解决的办法有两个:
1. 设定函数FoundTheTR();该函数用来向上追溯娃娃的老爹爷爷们,直到这个碰到个TR元素为止。这个方法是偶用过的,包试包灵。
2。还没有时间去发掘,大约就是找个办法把那个this给塞进去。
用函数逐级追溯吧,别愁了。
在TR或者TD级别设置style!
var oTR=FoundTR(event.srcElement);
if(!oTR)return false;
oTR.style.backgroundColor=top.cC2;
return true;
}
function FoundTR(oSRC){
var i=0;
while(oSRC.tagName!="TR"){
i++;if(i>10){alert('出现严重兼容问题!');return null;}
oSRC=oSRC.parentNode;
}
return oSRC;
}
我觉得你对文字的定义放到<td>里面去就行了
<td nowrap style="color:#000080;font-size:25pt">gfgdfg</td>
多谢lansajf(海)!!!!!!!!!!!!!!!!!!!!!!!!!!