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]);};
{
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]);};
或者用jquery 更简单
window.onload=function()
{
if(table.rows.length>1)
{
table.rows[1].click();
}
}
出错:table.rows为空或不是对象。
{
var table=document.getElementById("你的tableID");
if(table.rows.length>1)
{
table.rows[1].click();
}
}
我试一下,我是用datagrid绑定的。
var table=document.getElementById("<%=DataGridView1.ClientID%>");
onload时候参数为第一行的id
发现你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;//加上这一句就可以了
}
window.onload=function()
{
var table=document.getElementById("<%=selectBuyersGrid.ClientID%>");
if(table.rows.length>1)
{
table.rows[1].click();
}
}
function ChangePage(KeyCode)
{ if (KeyCode == 38)
{
if(currowno>0) //这里改成>0而不是>1试试
{
//使用键盘上下键移动记录
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);
}
}
//使用键盘上下键移动记录
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);
}
}
window.onload=function()
{
var table=document.getElementById("selectBuyersGrid");
if(table.rows.length>1)
{
table.rows[1].click();
document.onkeydown=function()
{
ChangePage(event.keyCode)
}
}
}