set id before insert
then set event by its id

解决方案 »

  1.   

    使用attachEvent给对象添加事件
      

  2.   

    var row = tab1.insertRow(0); 
    row.setAttribute("id","awaysrian");
    row.attachEvent("onclick",new Function("test()"));
      

  3.   

    var row = tab1.insertRow(0); 
    row.setAttribute("id","awaysrian");//设置属性
    row.attachEvent("onclick",new Function("test()"));//添加注释
      

  4.   

    还是有问题:
    我又一个表格,它的行是动态添加的,添加一行以后,我用如下添加事件:
    row = table1.insertRow(1); 
    row.attachEvent("onmouseover",new Function("test(this)"));
    row.attachEvent("onmouseout",new Function("test(this)"));
    调用的test函数如下:
    function test(obj) {
    if (g_Color == "") 
    {
    g_Color = obj.bgColor;
    obj.bgColor = "#FF9999";
    }
    else
    {
    obj.bgColor = g_Color;
    g_Color = "";
    }
    }g_Color是一个全局变量。鼠标载表格上移动没有任何效果???应该会改变背景色的亚!
      

  5.   

    参数this是不是也是这样给:row.onmouseover=test(this);
      

  6.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN">
    <HTML>
    <HEAD>
    <TITLE> Q2413197 </TITLE>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <META NAME="Generator" CONTENT="Microsoft Notepad">
    <META NAME="Author" CONTENT="Topmonkey">
    <META NAME="Keywords" CONTENT="http://expert.csdn.net/Expert/topic/2413/2413197.xml?temp=.3201258">
    <META NAME="Description" CONTENT="Re: Q2413197">
    </HEAD><BODY bgcolor="#FFFFFF" leftmargin="0" topmargin="0"><Table id=table1 Width="300" Align="Center" Border="1">
    <tr>
    <td>aaaaaaaaaaaaaa</td>
    </tr>
    </Table>
    <Script Language="JavaScript">
    <!--
    g_Color = "";
    function test(obj) {
    if (g_Color == ""){
    g_Color = event.srcElement.style.background;
    event.srcElement.style.background="#6699ff";
    }else{
    event.srcElement.style.background = g_Color;
    g_Color = "";
    }
    }
    row = table1.insertRow(-1); 
    row.attachEvent("onmouseover",new Function("test(this)"));
    row.attachEvent("onmouseout",new Function("test(this)"));
    cel = row.insertCell(-1);
    cel.innerHTML = "bbbbbbbbbb";
    cel.style.background = "#FFFFFF";
    //-->
    </Script>
    </BODY>
    </HTML>