也就是在onclick事件时,得到是在哪一行的哪一列产生的事件?

解决方案 »

  1.   

    <table id="wc" width="150" border="1">
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
    </table>
    <script type="text/javascript">
    window.onload = function () {
    var wc = document.getElementById("wc").getElementsByTagName("tbody")[0], r = wc.rows, c, i, j;
    for (i = 0 ; i < r.length ; i ++) {
    for (j = 0 ; j < r[i].cells.length ; j ++) {
    c = r[i].cells[j];
    c.onclick = function () {
    var wc = this;
    alert("0开始记数:\n行:" + wc.parentNode.rowIndex + "\n列:" + wc.cellIndex);
    };
    }
    }
    };
    </script>
      

  2.   

    L@_@K! <body>
      <table id="tabTest">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr>
            <td>4</td>
            <td>5</td>
            <td>6</td>
        </tr>
        <tr>
            <td>7</td>
            <td>8</td>
            <td>9</td>
        </tr>
      </table>
    <script type="text/javascript">
    <!--
    var oTable = document.getElementById("tabTest");
    var colCells = oTable.cells;
    for (var i=0; i<colCells.length; i++)
    {
        colCells[i].onclick = function ()
        {
            alert("行" + (this.parentNode.rowIndex + 1) + " : 列" + (this.cellIndex + 1));
        }
    }
    //-->
    </script>
     </body>
      

  3.   

    顶,问题还未解决,我的事件是在 <tr>
                                                    <td ><nobr> 工資率</td>
                                                    <td ><nobr>元/時</td>
                                                    <td ><input type="text" name="textfield3" onclick="Gettotal()" width="50px" size="4"></td></tr>写的?
      

  4.   

    完全可以!L@_@K! <body>
      <table id="tabTest">
        <tr>
            <td ><nobr> 工資率</td>
            <td ><nobr>元/時</td>
            <td ><input type="text" name="textfield3" width="50px" size="4"></td>
        </tr>
        <tr>
            <td ><nobr> 工資率</td>
            <td ><nobr>元/時</td>
            <td ><input type="text" name="textfield3" width="50px" size="4"></td>
        </tr>
      </table>
    <script type="text/javascript">
    <!--
    var oTable = document.getElementById("tabTest");
    var colCells = oTable.cells;
    for (var i=0; i<colCells.length; i++)
    {
        colCells[i].onclick = function ()
        {
            alert("行" + (this.parentNode.rowIndex + 1) + " : 列" + (this.cellIndex + 1));
        }
    }
    //-->
    </script>
     </body>
      

  5.   

    <table id="wc" width="150" border="1">
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
    </table>
    <table width="150" border="1">
      <tr>
        <td ><nobr> 工資率</td>
        <td ><nobr>元/時</td>
        <td ><input type="text" name="textfield3" onclick="Gettotal()" width="50px" size="4"></td>
      </tr>
    </table>
    <script type="text/javascript">
    var show = function (wc) {
        alert("0开始记数:\n行:" + wc.parentNode.rowIndex + "\n列:" + wc.cellIndex);
    };
    window.onload = function () {
        var wc = document.getElementById("wc").getElementsByTagName("tbody")[0], r = wc.rows, c, i, j;
        for (i = 0 ; i < r.length ; i ++) {
            for (j = 0 ; j < r[i].cells.length ; j ++) {
                c = r[i].cells[j];
                c.onclick = function () {
                    show(c);
                };
            }
        }
    };
    </script>
      

  6.   

    忘记加进去了。。
    <table id="wc" width="150" border="1">
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
    </table>
    <table width="150" border="1">
      <tr>
        <td onClick="show(this)"><nobr> 工資率</td>
        <td onClick="show(this)"><nobr>元/時</td>
        <td onClick="show(this)"><input type="text" name="textfield3" onclick="Gettotal()" width="50px" size="4"></td>
      </tr>
    </table>
    <script type="text/javascript">
    var show = function (wc) {
        alert("0开始记数:\n行:" + wc.parentNode.rowIndex + "\n列:" + wc.cellIndex);
    };
    window.onload = function () {
        var wc = document.getElementById("wc").getElementsByTagName("tbody")[0], r = wc.rows, c, i, j;
        for (i = 0 ; i < r.length ; i ++) {
            for (j = 0 ; j < r[i].cells.length ; j ++) {
                c = r[i].cells[j];
                c.onclick = function () {
                    show(c);
                };
            }
        }
    };
    </script>
      

  7.   

    我自己写的一个取行号的(IE适用):
    function GetRowIndex(elt)
    {
    for( var tr = elt; tr && tr.parentElement != tr; tr = tr.parentElement )
    {
    if( tr.tagName == "TR" )
    {
    return tr.rowIndex;
    }
    }
    return -1;
    }
      

  8.   

    忽忽。。真是辛苦呢。。同情ing..我们这里不让上Q了。。前几天刚卸掉。。