table的数据是动态加载的,table是放在datalist中的
如何获取表格的最后一行?使其突出显示
<script language="javascript">
function TBtr(rowNum)
{
var tb=document.getElementById('tb2');
tb.rows[rowNum].style.color='green';
}
</script>后台
‘------------------------------------
  绑定数据代码
  Page.RegisterStartupScript("", "<script>TBtr('" & Adst.Tables("ProSrRe").Rows.Count - 1 & "');</script>")能得到rowNum的值
提示rows[].style不存在或不是对象。

解决方案 »

  1.   

    首先你应该先alert 一下当前rowNum是多少
    或者试用tb.lastChild 访问该元素试试
      

  2.   

    执行这条语句的时候Table还没有行,也就是说rowNum是-1
      Page.RegisterStartupScript("", " <script>TBtr('" & Adst.Tables("ProSrRe").Rows.Count - 1 & "'); </script>") 
    把Page这句删掉
    在body加
    <body onload="TBtr(document.getElementById('tb2').rows.length-1)">
      

  3.   

    呵呵,我试了,最后一行字体颜色是会变的,如果你是想让行背景变色的话 tb.rows[rowNum].style.backgroundColor='green'; 
      

  4.   

    不行呀!是不是你的table数据是固定的?要不把你的测试代码给我,谢谢
      

  5.   

    你用alert(rowNum);
    看一下rowNum是多少不就行了~
      

  6.   

    Page_Load里面
    if(!Page.IsPostBack)
    {
       GridView1.DataSource = 数据源;
       GridView1.DataBind();
    }在页面里面
    <script language="javascript"> 
    function TBtr(rowNum) 

    var tb=document.getElementById('GridView1'); 
    tb.rows[rowNum].style.color='green'; 

    </script> <body onload="TBtr(document.getElementById('GridView1').rows.length-1)"> 
      

  7.   

    你这种方法,用alert(rowNum)只能得到0,我的方法可以得到行数,但却提示rows[].style不存在或不是对象
      

  8.   

    我确定我的是ok的,要不你把"table的数据是动态加载的"的意思再说明清楚吧?
      

  9.   


    <ItemTemplate>
    <table id="tb2" cellspacing="0">
    <tr onmouseover="this.style.backgroundColor='#00ff00';this.style.color='#ff0000';this.style.cursor='hand'"
    onmouseout="this.style.color='';this.style.backgroundColor=''">
    <td width="8%" style="border-left:1px solid;border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.生管回復交期") %>' ID="Label1">
    </asp:Label></td>
    <td width="3%">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.序號") %>' ID="Label51">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.派工日期") %>' ID="Label2">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.未交貨數量") %>' ID="Label3">
    </asp:Label></td>
    <td width="16%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.NCT生產進度") %>' ID="Label4">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.預計NCT完成") %>' ID="Label9">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.發烤漆日期") %>' ID="Label5">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.回廠日期") %>' ID="label10">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.[包材/線材]") %>' ID="Label6">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.預計組裝日期") %>' ID="Label7">
    </asp:Label></td>
    <td width="10%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.預計組裝進度") %>' ID="Textbox3">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.排程人") %>' ID="Label8">
    </asp:Label></td>
    </tr>
    </table>
    </ItemTemplate>  ''接受參數制令單號
                    Dim ZLDH As String = Request.QueryString("ZLDH")
                    Astrsql = "select 序號,排程人,生管回復交期,派工日期,未交貨數量,NCT生產進度,預計NCT完成,發烤漆日期,回廠日期,[包材/線材],預計組裝日期,預計組裝進度 from T_ProPlanSched where 制令單號='" & ZLDH & "' order by 排程日期 asc"
                    Aadp = New SqlDataAdapter(Astrsql, Gcon)
                    Aadp.Fill(Adst, "ProSrRe")
                    dlProPlanSched.DataSource = Adst.Tables("ProSrRe")
                    dlProPlanSched.DataBind()
      

  10.   

    天啊,你竟然把table放在</ItemTemplate> 里面,那会生成n个table
    ,把tr放里面就可以了
      

  11.   

        <table>
          <asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
              <tr><td><asp:Label ID="Label1" Text='<%# DataBinder.Eval(Container, "DataItem.預計組裝日期") %>' runat="server"/></td></tr>
            </ItemTemplate>
          </asp:DataList>
        </table>
      

  12.   

    LZ自己测试一下看在哪个地方出错,先alert(tb),看能不能获取table对象,照楼主的设计,我觉得不大可能,因为将table放在datalist中,会出现n个table,id就不唯一了。如果能取到,在测试其属性。
      

  13.   

    <asp:datalist id="dlProPlanSched" runat="server" Width="100%">
    <HeaderTemplate>                            <table id="tb2" cellspacing="0">
    </HeaderTemplate>

    <ItemTemplate>
    <TR onmouseover="this.style.backgroundColor='#00ff00';this.style.color='#ff0000';this.style.cursor='hand'"
    onmouseout="this.style.color='';this.style.backgroundColor=''">
    <td width="8%" style="border-left:1px solid;border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.生管回復交期") %>' ID="Label1">
    </asp:Label></td>
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid">
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.派工日期") %>' ID="Label2">
    </asp:Label></td> </TR>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:datalist>这样html显示有错误(要么缺少结束标记,要么与‘HeaderTemplate’重叠)
    运行后alert(rowNum)能显示实际行数,但每次都是中间行的背景颜色变绿。
      

  14.   

     <table id="tb2" cellspacing="0"> 显示错误。
    当有12行,第六行就变绿,为什么呢?
      

  15.   

    这种方法不行:提示rows[].style不存在或不是对象
      

  16.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Error</title>
        <script src="js/jquery-1.3.2.js" type="text/javascript"></script>
        <script type="text/javascript" >
       $(document).ready(function(){
        $("tr:last").css("color","green");
    });
    </script>
     
    </head>
    <body>
    <table></table></body>
    </html>
      

  17.   

    如果是特定表格<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>test</title>
        <script src="js/jquery-1.3.2.js" type="text/javascript"></script>
        <script language="javascript">
        $(document).ready(function(){
          $("#tb2").find("tr:last").css("color","red");
        });
        </script>
    </head>
    <body>
    <table id="tb2">
    <tr><td>测试数据1</td></tr>
    <tr><td>测试数据2</td></tr>
    <tr><td>测试数据3</td></tr>
    </table>
    </body>
    </html>
      

  18.   


    function changeColor()
    {
    var tbs = document.getElementsByTagName("table");
    var count = 0;
    for( i = 0;i < tbs.length;i++)
    {
    if(tbs[i].id == "tb2")
    {
    count++;
    }
    }
    tbs[count].rows[0].style.backgroundColor = "blue";
    }
      

  19.   

    哦,差点忘了,在页面最后面插入<script language="javascript">changeColor()</script>或者加在body的onload事件里面就行了
    have a try!@!
      

  20.   


    table套在外面 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<table id="tb2" cellspacing="0"> 
    <asp:datalist id="dlProPlanSched" runat="server" Width="100%"> 
    <HeaderTemplate> 
    </HeaderTemplate> 
    <ItemTemplate> 
    <TR onmouseover="this.style.backgroundColor='#00ff00';this.style.color='#ff0000';this.style.cursor='hand'" 
    onmouseout="this.style.color='';this.style.backgroundColor=''"> 
    <td width="8%" style="border-left:1px solid;border-bottom:1px solid;border-right:1px solid"> 
    <asp:Label runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.生管回復交期") %>' ID="Label1"> 
    </asp:Label> </td> 
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid"> 
    <asp:Label runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.派工日期") %>' ID="Label2"> 
    </asp:Label> </td> 
    </TR>
    </ItemTemplate> 
    <FooterTemplate> 
    </FooterTemplate> 
    </asp:datalist> 
    </table> 
      

  21.   

    table套在外面html仍然有提示错误。并且alert(rowNum)为零,
    asp:datalist id="dlProPlanSched" runat="server" Width="100%"> 
    <HeaderTemplate>                           <table id="tb2" cellspacing="0"> 
    </HeaderTemplate> <ItemTemplate> 
    <TR onmouseover="this.style.backgroundColor='#00ff00';this.style.color='#ff0000';this.style.cursor='hand'" 
    onmouseout="this.style.color='';this.style.backgroundColor=''"> 
    <td width="8%" style="border-left:1px solid;border-bottom:1px solid;border-right:1px solid"> 
    <asp:Label runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.生管回復交期") %>' ID="Label1"> 
    </asp:Label> </td> 
    <td width="8%" style="border-bottom:1px solid;border-right:1px solid"> 
    <asp:Label runat="server" Text=' <%# DataBinder.Eval(Container, "DataItem.派工日期") %>' ID="Label2"> 
    </asp:Label> </td> 
    </TR> 
    </ItemTemplate> 
    <FooterTemplate> 
    </table> 
    </FooterTemplate> 
    </asp:datalist> 但是我的办法经过tb.rows[rowNum*2+1].style.color='green'; 竟然能行了,不知道为什么啊?
      

  22.   

    谢谢楼主,你的办法改成tbs[count+1].rows[0].style.backgroundColor = "blue";就可以了,反正问题很怪呀。
      

  23.   

    呵呵,不会吧,楼主,你是不是还有其他地方的table的id定义成tb2了???
      

  24.   


    用JQUERY的选择器吧,很方便的。 19楼的就是用的JQUERY。。 没有你那样麻烦的~~