在一个静态的TABLE里,我这样设又是正确的:
<script language="javascript">
  function key(object)
{
alert(object.id);
object.id += object.id;
}
</script>
<P>
<TABLE id="tab" cellSpacing=1 cellPadding=1 width="75%" border=1>
  
  <TR>
    <TD id="abc" onclick="key(this)">1</TD>
    <TD>2</TD>
    <TD>3</TD></TR>
  <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD></TR>
  <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD></TR></TABLE></P>转到动态生成的TABLE里,为什么就不行了呢?查资料insertCell()返回的确实是一个<TD>对象啊....

解决方案 »

  1.   

    <body>
    <table>
    <tr id='p'></tr>
    </table>
    <script>
    debugger;
    oCell = document.all('p').insertCell();
    oCell.innerText='p';
    oCell.attachEvent("onclick",aa);
    oCell.Width = 120;
    oCell.Height = 90;
    //oCell.innerText = i * colcount + j + "";
    //if  ((i * colcount + j) < IconCount)
    //{
    //oCell.id = "PIC_" + i * colcount + j;
    oCell.id='p';
    function aa(){
    alert(event.srcElement.id);
    }
    </script></body>
      

  2.   

    是你的脚本没有写对,试试我的:<TABLE id="tab" cellSpacing=1 cellPadding=1 width="75%" border=1>
      <TR>
        <TD id="abc" onclick="key()">1</TD>
        <TD>2</TD>
        <TD>3</TD></TR>
    </TABLE><INPUT TYPE="button" value="加入一行" onclick="test()"><SCRIPT LANGUAGE="JavaScript">
    <!--
    function key(){
        var object=event.srcElement
        if(object.id){
            alert(object.id);
        }
    }
    function test(){
        var oRow1=tab.insertRow()
        var colcount=tab.rows[0].cells.length
        if(!colcount){colcount=3}
        var j=oRow1.rowIndex
        var IconCount=100
        for(var i=0;i<colcount;i++){
            oCell = oRow1.insertCell();
            oCell.width = 120;  //这里的width要小写
            oCell.height = 90;  //这里的height要小写
            oCell.innerText = i * colcount + "" + j;
            if((i * colcount + j) < IconCount){
                oCell.id = "PIC_" + i * colcount + j;
            }
            oCell.onclick=key   //为这个单元格指定onclick事件函数
        }
    }
    //-->
    </SCRIPT>给分吧,呵呵:D!