那原理是什么?我试了onclick事件,貌似没效果

解决方案 »

  1.   

    <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>
      

  2.   


    用jquery兼容好点,绑定click事件,改变td的背景色
    几行几列,$('td').index()这样试试
      

  3.   

    直接运行就可以了。<!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>
      

  4.   

    Quote: 引用 5 楼 fdipzone 的回复:

    膜拜