请问各位JS高手,对于静态表格的单元格怎么取值啊?
如下表格:
目前想要实现的功能是,在鼠标在表格中的111和222列时,111和222列会变色凸显且鼠标为手型,点击111和222列范围内(可理解111和222合并表示一个值)的任意位置,则直接链接到另一个网页。但是其中还有一个判断,当111和222列中的值不为N/A时,才有以上的效果。
看了版上的一些例子,不过很多都是用JS动态生成表格的,但是我这里不需要。目前我考虑的是在列111和222<TD>中添加onmousemove()和onmouseclick(this)的事件来处理,但是不知道该如何通过this来获得当前单元格内的值?请各位大侠指教,或者有更好的方法处理的话也希望赐教。
另外列列111和222中的边框能不能换颜色的啊?对于单元格的处理在下真没什么好方法,谢谢
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<BODY>
<TABLE border=1>
<TR>
<TD>AAA</TD>
<TD>BBB</TD>
<TD>111</TD>
<TD>222</TD>
<TD>CCC</TD>
</TR>
</TABLE>
</BODY>
</HTML>
问题点数:100、回复次数:5Top
1 楼AitStudio(轻罗小扇)回复于 2005-08-28 00:09:12 得分 5
没有onmouseclick这个事件的,只有onclick.
看看这个行不行:onclick="if(!this.innerHTML)='';"Top
2 楼zhaoxiaoyang(梅雪香@深圳)回复于 2005-08-28 00:10:46 得分 40
<HTML>
<head>
<style type="text/css">
td{ text-align:center}
.tdOver{border:1px solid;border-color: buttonface threeddarkshadow threeddarkshadow buttonface;cursor:hand;
}
</style>
<script language="javascript">
function tdOver(curTd){
curTd.className="tdOver";
}
function tdOut(curTd){
curTd.className="td";
}
function tdClick(curTd){
var tdVal=curTd.innerText;
if(tdVal=="N/A") return false;
window.location="http://www.163.com";
}
</script>
</head>
<BODY>
<TABLE border=1 width="450">
<TR>
<TD>AAA</TD>
<TD>BBB</TD>
<TD onMouseOver="tdOver(this)" onMouseOut="tdOut(this)" onClick="tdClick(this)">111</TD>
<TD onMouseOver="tdOver(this)" onMouseOut="tdOut(this)" onClick="tdClick(this)">222</TD>
<TD>CCC</TD>
</TR>
</TABLE>
</BODY>
</HTML>Top
3 楼qidizi(qidizi)回复于 2005-08-28 18:41:18 得分 5
你要是已知要的范围:可以当鼠标发生移动时检测当前的对象,是TR时就检测它的TABLEINDEX是不是在111 - 1 --> 222-1内,是话再检测是不是空,否则就什么!Top
4 楼njtucomputer(冬虫草)回复于 2005-08-28 22:59:25 得分 50
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<BODY>
<TABLE border=1 id="table1">
<TR>
<TD>AAA</TD>
<TD>BBB</TD>
<TD onclick="clickIt(this)">111</TD>
<TD onclick="clickIt(this)">222</TD>
<TD>CCC</TD>
</TR>
</TABLE>
</BODY>
</HTML>
<script>
function window.onload(){
table1.onmouseover = overIt;
table1.onmouseout = outIt;
}
function overIt(){
var the_td = get_Element(event.srcElement,"td");
if(the_td == null) return;
var the_tr = the_td.parentElement;
if(the_tr.rowIndex == 0){
for(var i = 0 ; i < the_tr.cells.length; i++){
if(i == 2 || i == 3){
with(the_tr.cells[i]){
runtimeStyle.backgroundColor ="#BEC5DE";
style.cursor = "hand";
}
}
}
}
}
function outIt(){
var the_td = get_Element(event.srcElement,"td");
if(the_td == null) return;
var the_tr = the_td.parentElement;
if(the_tr.rowIndex == 0){
for(var i = 0 ; i < the_tr.cells.length; i++){
if(i == 2 || i == 3){
with(the_tr.cells[i]){
runtimeStyle.backgroundColor ="";
}
}
}
}
}
function get_Element(the_ele,the_tag){
the_tag = the_tag.toLowerCase();
if(the_ele.tagName.toLowerCase()==the_tag)return the_ele;
while(the_ele=the_ele.offsetParent){
if(the_ele.tagName.toLowerCase()==the_tag)return the_ele;
}
return(null);
}
function clickIt(curTd){
var tdVal=curTd.innerText;
if(tdVal=="N/A") return false;
window.location="other.htm";
}
</script>
看看是不是你要的?Top
5 楼dreamheaven(juvei)回复于 2005-08-28 23:33:56 得分 0
谢谢各位的帮助,问题解决了,大家真是热心肠啊Top




