function selectRow(td)
    {
      if(table!=null && oldrowno!=null)
      {
          table.rows(oldrowno).style.backgroundColor=oldColor;
      }
if(td)
{
          currowno=td.parentNode.rowIndex;
         
          CurrowRow=td.parentNode;
          oldrowno=currowno;
}
      else if(event.srcElement.tagName.toLowerCase()=='td')
      {
          if(table==null)
          {
            table=event.srcElement.parentNode.parentNode;
          }
          currowno=event.srcElement.parentNode.rowIndex;
         
          CurrowRow=event.srcElement.parentNode;
          oldrowno=currowno;
      }
      else if(event.srcElement.tagName.toLowerCase()=='tr')
      { 
          if(table==null)
          {
              table=event.srcElement.parentNode;
          }
          currowno=event.srcElement.rowIndex;
          CurrowRow=event.srcElement;
          oldrowno=currowno;
      }
      if(CurrowRow!=null)
      {
          oldColor=CurrowRow.style.backgroundColor;
          CurrowRow.style.backgroundColor=newColor;
      }   
    } 
window.onload = function(){selectRow(table.rows(0).cells[0]);};

解决方案 »

  1.   

    加个函数,简单的很 onload的加载
    或者用jquery 更简单 
      

  2.   

    晕,有见了,怎么又用这个代码了
    window.onload=function()
    {
      if(table.rows.length>1)
      {
         table.rows[1].click();
      }
    }
      

  3.   

    在body标签中加入 onload=javascript:table.rows[1].click();
      

  4.   


    出错:table.rows为空或不是对象。
      

  5.   

    table定义一下var table=document.getElementById("你的tableID");
      

  6.   

    window.onload=function()
    {
      var table=document.getElementById("你的tableID");
      if(table.rows.length>1)
      {
         table.rows[1].click();
      }
    }
      

  7.   


    我试一下,我是用datagrid绑定的。
      

  8.   

    用ClientID保险一点,因为在masterpage里,ClientID和ID不同
    var table=document.getElementById("<%=DataGridView1.ClientID%>"); 
      

  9.   

    selectRow()里面你可以设置一个参数
    onload时候参数为第一行的id
      

  10.   

    谢谢各位帮忙载入后第一行记录被选中    已经解决,我现在只需要,按上下键时,能从第一行往下移动就可以了。也就是触发 ChangePage(KeyCode) 
      

  11.   

     
    发现你selectRow()这个函数,有个问题,oldrow没赋值,因为你下面向下移动时
    if(table!=null && currowno <table.rows.length && oldrowno!=null && oldrowno <table.rows.length-1) 
    这里有个判断oldrowno!=null,所以不执行
    改一下selectRow()
    if(event.srcElement.tagName.toLowerCase()=='tr') 
          {  
              if(table==null) 
              { 
                  table=event.srcElement.parentNode; 
              } 
              currowno=event.srcElement.rowIndex; 
              CurrowRow=event.srcElement; 
              oldrowno=currowno; 
              oldrow = CurrowRow;//加上这一句就可以了
          } 
      

  12.   

    鼠标在记录上单击(onclick事件),调用selectRow() ,然后再按上下键移动记录(onkeydown事件)ChangePage(KeyCode),这样就可以正常运行.用下面的方法:可以让表格的第一行,高亮显示,但是按上下键,不能移动记录.必须得点击一行,才可以用上下键移动.
           window.onload=function()
           {
               var table=document.getElementById("<%=selectBuyersGrid.ClientID%>"); 
               if(table.rows.length>1)
               {
                   table.rows[1].click();
               }
            }
      

  13.   

    附件我不能下载,
    function ChangePage(KeyCode) 
        {         if (KeyCode == 38) 
            { 
              if(currowno>0) //这里改成>0而不是>1试试
              { 
      

  14.   

    我试过了,改成0就可以了
    //使用键盘上下键移动记录
        function ChangePage(KeyCode)
        {
     
            if (KeyCode == 38)
            {
               if(currowno>0)//就这里改成0
               {
                  if(table!=null && currowno>1 && oldrowno!=null)
                  {
                     table.rows(oldrowno).style.backgroundColor=oldColor;
                  }
                  currowno--;
                  if(table!=null && currowno>0)
                  {
                     oldColor=table.rows(currowno).style.backgroundColor;
                     table.rows(currowno).style.backgroundColor=newColor;
                  }
                  oldrowno=currowno;
               }
               
            }
            if (KeyCode == 40 )
            {
               if(currowno<table.rows.length-1)
               {
                  
                  if(table!=null && currowno<table.rows.length && oldrowno!=null && oldrowno<table.rows.length-1)
                  {
                     table.rows(oldrowno).style.backgroundColor=oldColor;;
                  } 
                  currowno++;
                  if(table!=null && currowno<table.rows.length)
                  {
                     oldColor=table.rows(currowno).style.backgroundColor;
                     table.rows(currowno).style.backgroundColor=newColor;
                  }
                  oldrowno=currowno;
               }
               
            }
            if (KeyCode == 13) 
            { 
               //打回车处理           
               //event.returnValue   =   false;
               window.opener.location.href="../contract/contractLr.aspx?buyersId="+table.rows(currowno).cells[0].innerText+"&buyersName="+table.rows(currowno).cells[1].innerText;
               //alert(table.rows(currowno).cells[0].innerText);
            }
        }
      

  15.   

    代码写的有点乱,改了你这个函数,测试成功
    //使用键盘上下键移动记录
        function ChangePage(KeyCode)
        {
     
            if (KeyCode == 38)
            {
               if(currowno>1)
               {
                  if(table!=null )
                  {
                     //if(currowno!=1)
                        table.rows(oldrowno).style.backgroundColor=oldColor;
                     currowno--;
                  }
                  
                  if(table!=null && currowno>0)
                  {
                     oldColor=table.rows(currowno).style.backgroundColor;
                     table.rows(currowno).style.backgroundColor=newColor;
                  }
                  oldrowno=currowno;
               }
               
            }
            if (KeyCode == 40 )
            {
               if(currowno<table.rows.length-1)
               {
                  if(table!=null  && oldrowno!=null && oldrowno<table.rows.length-1)
                  {
                     table.rows(oldrowno).style.backgroundColor=oldColor;;
                  } 
                  currowno++;
                  if(table!=null && currowno<table.rows.length)
                  {
                     oldColor=table.rows(currowno).style.backgroundColor;
                     table.rows(currowno).style.backgroundColor=newColor;
                  }
                  oldrowno=currowno;
               }
               
            }
            if (KeyCode == 13) 
            { 
               //打回车处理           
               //event.returnValue   =   false;
               window.opener.location.href="../contract/contractLr.aspx?buyersId="+table.rows(currowno).cells[0].innerText+"&buyersName="+table.rows(currowno).cells[1].innerText;
               //alert(table.rows(currowno).cells[0].innerText);
            }
        }
      

  16.   

    window.onload再加上document.onkeydown事件
    window.onload=function()
           {
               var table=document.getElementById("selectBuyersGrid"); 
               if(table.rows.length>1)
               {
                   table.rows[1].click();
                   document.onkeydown=function()
                   {
                        ChangePage(event.keyCode)
                   }
               }
            }