var table=document.getElementById('<%=gridView.ClientID%>'); var trs=table.getElementsByTagName('tr'); for(var i=0;i<trs.length;i++){ var tr=trs[i]; tr.onmouseover=function(){ this.style.background='#000'; } tr.onmouseout=function(){ this.style.background='#fff'; } }
经过ie6.0测试: c#: protected void GridView1_RowDataBound ( object sender, GridViewRowEventArgs e ) { if ( e.Row.RowType == DataControlRowType.DataRow ) { for ( int i = 0; i < e.Row.Cells.Count; i++ ) { e.Row.Cells [ i ].Attributes.Add ( "onmouseover", "changeColor(this)" ); e.Row.Cells [ i ].Attributes.Add ( "onmouseout", "cancelColor(this)" ); } } } javascript: function changeColor(oTd) { var rowIndex = oTd.parentNode.rowIndex; var cellIndex = oTd.cellIndex; var table1 = document.getElementById("<%=GridView1.ClientID%>"); var rows = table1.rows; if(table1.lastRowIndex!=undefined && table1.lastCellIndex!=undefined) { for(i=0; i<rows.length; i++) { if(i==table1.lastRowIndex) { rows[i].style.backgroundColor=""; continue; } var cells = rows[i].cells; for(j=0; j<cells.length; j++) { if(j==table1.lastCellIndex) { cells[j].style.backgroundColor=""; break; } } } } for(i=0; i<rows.length; i++) { if(i==rowIndex) { rows[i].style.backgroundColor="#FF5533"; continue; } var cells = rows[i].cells; for(j=0; j<cells.length; j++) { if(j==cellIndex) { cells[j].style.backgroundColor="#FF5533"; break; } } } table1.lastRowIndex = rowIndex; table1.lastCellIndex = cellIndex; } function cancelColor(oTd) { var table1 = document.getElementById("<%=GridView1.ClientID%>"); var rows = table1.rows; if(table1.lastRowIndex!=undefined && table1.lastCellIndex!=undefined) { for(i=0; i<rows.length; i++) { if(i==table1.lastRowIndex) { rows[i].style.backgroundColor=""; continue; } var cells = rows[i].cells; for(j=0; j<cells.length; j++) { if(j==table1.lastCellIndex) { cells[j].style.backgroundColor=""; break; } } } } }
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#B9D9FB\"");
}
}
{
e.Row.Attributes["onmouseover"] = "javascript:bgcolor=this.style.backgroundColor;this.style.backgroundColor='lemonchiffon';";
e.Row.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=bgcolor;";
foreach (TableCell c in e.Row.Cells)
{
c.Attributes["onmouseover"] = "javascript:bgcolor=this.style.backgroundColor;this.style.backgroundColor='red';";
c.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=bgcolor;";
}
}
其實1樓的回答并不是我想要的結果
我想要的是一個類似十字架的效果
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#B9D9FB\"");
}
}
-----------------------
支持
//改为
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#B9D9FB\";this.style.cursor='crosshair'");
var table=document.getElementById('<%=gridView.ClientID%>');
var trs=table.getElementsByTagName('tr');
for(var i=0;i<trs.length;i++){
var tr=trs[i];
tr.onmouseover=function(){
this.style.background='#000';
}
tr.onmouseout=function(){
this.style.background='#fff';
}
}
c#:
protected void GridView1_RowDataBound ( object sender, GridViewRowEventArgs e )
{
if ( e.Row.RowType == DataControlRowType.DataRow )
{
for ( int i = 0; i < e.Row.Cells.Count; i++ )
{
e.Row.Cells [ i ].Attributes.Add ( "onmouseover", "changeColor(this)" );
e.Row.Cells [ i ].Attributes.Add ( "onmouseout", "cancelColor(this)" );
}
}
}
javascript:
function changeColor(oTd)
{
var rowIndex = oTd.parentNode.rowIndex;
var cellIndex = oTd.cellIndex;
var table1 = document.getElementById("<%=GridView1.ClientID%>");
var rows = table1.rows;
if(table1.lastRowIndex!=undefined && table1.lastCellIndex!=undefined)
{
for(i=0; i<rows.length; i++)
{
if(i==table1.lastRowIndex)
{
rows[i].style.backgroundColor="";
continue;
}
var cells = rows[i].cells;
for(j=0; j<cells.length; j++)
{
if(j==table1.lastCellIndex)
{
cells[j].style.backgroundColor="";
break;
}
}
}
}
for(i=0; i<rows.length; i++)
{
if(i==rowIndex)
{
rows[i].style.backgroundColor="#FF5533";
continue;
}
var cells = rows[i].cells;
for(j=0; j<cells.length; j++)
{
if(j==cellIndex)
{
cells[j].style.backgroundColor="#FF5533";
break;
}
}
}
table1.lastRowIndex = rowIndex;
table1.lastCellIndex = cellIndex;
}
function cancelColor(oTd)
{
var table1 = document.getElementById("<%=GridView1.ClientID%>");
var rows = table1.rows;
if(table1.lastRowIndex!=undefined && table1.lastCellIndex!=undefined)
{
for(i=0; i<rows.length; i++)
{
if(i==table1.lastRowIndex)
{
rows[i].style.backgroundColor="";
continue;
}
var cells = rows[i].cells;
for(j=0; j<cells.length; j++)
{
if(j==table1.lastCellIndex)
{
cells[j].style.backgroundColor="";
break;
}
}
}
}
}
1`不是太麻烦!~
結貼啦,謝謝代碼