点击单元格,变背景颜色 那原理是什么?我试了onclick事件,貌似没效果 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script>$(document).ready(function() { $("td").click(function(){ //alert($(this).parents().index()); $(this).css("background-color","green"); alert($(this).parents("tr").prevAll().length+1); alert($(this).prevAll().length+1); }); });</script> 用jquery兼容好点,绑定click事件,改变td的背景色几行几列,$('td').index()这样试试 直接运行就可以了。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> test </title> </head> <body> <style type="text/css"> td{height:30px;} .hilite{background:#FFFF00;} </style> <table width="500" border="1" id="mtb"> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </table> <input type="button" value="显示高亮的单元格" onclick="showhilite()"> <script type="text/javascript"> var hilite = {}; var tbs = document.getElementById("mtb").getElementsByTagName('td'); for(var i=0,len=tbs.length; i<len; i++){ tbs[i].onclick = function(){ if(this.className!='hilite'){ this.className = 'hilite'; hilite[this.parentNode.rowIndex+'_'+this.cellIndex] = (this.parentNode.rowIndex+1)+'行'+ (this.cellIndex+1) + '列'; }else{ this.className = ''; delete hilite[this.parentNode.rowIndex+'_'+this.cellIndex]; } } } function showhilite(){ var tmp = ''; for(key in hilite){ tmp += hilite[key] + "\n"; } if(tmp==''){ alert('未选中任何单元格'); }else{ alert(tmp); } } </script> </body></html> Quote: 引用 5 楼 fdipzone 的回复:膜拜 flexpaper不能显示由doc转化而来的swf apache php mysql乱码 关于ajax的一个小问题 大家分析一下这个网站的时间 项目改为改为b/s架构,能否给点建议? 为什么连接不上MYSQL 用GD函数按百分比缩小一张图后图片的背景不完全透明!期望高手解决! php mysql 怎么知道某个会员哪些贴子是他没看过的。如何实现 php.ini 无法调出来ftp扩展 关于iframe的问题 想请教一下wampserver开gzip的问题 【求助】导航栏
$(document).ready(function() {
$("td").click(function(){
//alert($(this).parents().index());
$(this).css("background-color","green");
alert($(this).parents("tr").prevAll().length+1);
alert($(this).prevAll().length+1);
});
});
</script>
用jquery兼容好点,绑定click事件,改变td的背景色
几行几列,$('td').index()这样试试
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title> test </title>
</head> <body>
<style type="text/css">
td{height:30px;}
.hilite{background:#FFFF00;}
</style> <table width="500" border="1" id="mtb">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table> <input type="button" value="显示高亮的单元格" onclick="showhilite()"> <script type="text/javascript">
var hilite = {};
var tbs = document.getElementById("mtb").getElementsByTagName('td');
for(var i=0,len=tbs.length; i<len; i++){
tbs[i].onclick = function(){
if(this.className!='hilite'){
this.className = 'hilite';
hilite[this.parentNode.rowIndex+'_'+this.cellIndex] = (this.parentNode.rowIndex+1)+'行'+ (this.cellIndex+1) + '列';
}else{
this.className = '';
delete hilite[this.parentNode.rowIndex+'_'+this.cellIndex];
}
}
} function showhilite(){
var tmp = '';
for(key in hilite){
tmp += hilite[key] + "\n";
}
if(tmp==''){
alert('未选中任何单元格');
}else{
alert(tmp);
}
} </script>
</body>
</html>