隔行同色,行间隔色

 用css控制表格间隔色

tr{background:expression(this.rowIndex%2==0 ? "red":"")}

LI列表输出,隔行同色,行间隔色

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<style>
.mytable {border-collapse:collapse;border:solid #6AA70B;border-width:0px 0 0 0px;width:50%;}
.mytable ul li {padding-top:5px;text-indent:2em;list-style:none;background:url(http://www.blueidea.com/img/common/new_flash.gif) 3px 50% no-repeat;border-bottom:#6AA70B 1px dotted ;font-family: "Verdana,宋体";font-size: 12px;color:#008000;text-align:left;height:25px;}
.mytable ul li.t1 {background-color:#EFFEDD;}/* 第一行的背景色 */
.mytable ul li.t2{background-color:#ffffff;}/* 第二行的背景色 */
.mytable ul li.t3 {background-color:#D2FCA0;}/* 鼠标经过时的背景色 */
</style>
<body style=margin:0;><br><br><br>
<div align="center">
<div class=mytable id=tab>
<ul>
    <li><a href="#">这个是第1行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第2行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第3行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第4行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第5行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第6行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第7行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第8行的文字,大家看清楚了</a></li>
<li><a href="#">这个是第9行的文字,大家看清楚了</a></li>
</ul></div><script type="text/javascript">
<!--
var Ptr=document.getElementById("tab").getElementsByTagName("li");
function $() {
      for (i=1;i<Ptr.length+1;i++) {
      Ptr[i-1].className = (i%2>0)?"t1":"t2";
      }
}
window.οnlοad=$;
for(var i=0;i<Ptr.length;i++) {
      Ptr<i>.οnmοuseοver=function(){
      this.tmpClass=this.className;
      this.className = "t3";   
      };
      Ptr[i].οnmοuseοut=function(){
      this.className=this.tmpClass;
      };
}
//-->
</script>
</body>
</html>
 

阿牛实现的是可以控制表格的当前鼠标所在行的样式
其构思巧妙,解决了css的expression中耗尽客户端资源的问题
利用这样的构架,偶加入了间隔色以及鼠标选中的颜色,并且激发了一个js事件clickfunction

使用时设定表格class=dgInit
并加入选中行调用的函数
clickfunction=function(obj){
  //obj为当前选中的行object
  //如obj.cell[i].innerText可取到当前行第i列的文本
}

代码:

<style>
/*
DataGrid和Table的样式,可区分标题栏(table首行),间隔显示行,以及感应鼠标
*/
/*
总体的样式:未加载时定义js函数,加载完成后调用
//分单双数行定义间隔样式
//定义鼠标移上的样式
*/
body,div,form,label
{
  font-size:13px;
  
}
table tr td
{
  text-align:center;
  font-size:13px;
  
}
  

.dgInit
{

whoisyoung:expression(((this.readyState=="complete") && (this.className=="dg")) || (
(
init_tr=function(objTable)
{
objTable.rows[0].className="dg_tr_head";
var i;
last_tr_click=document;
lastClassName="";

for(i=1;i<objTable.rows.length;i++)
{
if(i%2!=0){
objTable.rows[i].className="dg_tr_normal";
objTable.rows[i].οnclick=function(){last_tr_click.className=lastClassName;lastClassName="dg_tr_normal";this.className="dg_tr_click";last_tr_click=this;clickfunction(this);};
objTable.rows[i].οnmοuseοut=function(){
if(this.className!="dg_tr_click")
{
this.className="dg_tr_normal"
}//end if

}//end function
}//end if
else{
objTable.rows[i].className="dg_tr_normal2";
objTable.rows[i].οnclick=function(){last_tr_click.className=lastClassName;lastClassName="dg_tr_normal2";this.className="dg_tr_click";last_tr_click=this;clickfunction(this);};
objTable.rows[i].οnmοuseοut=function()
{
if(this.className!="dg_tr_click")
{
this.className="dg_tr_normal2"
};

};

}
objTable.rows[i].indexName="row"+i;
//objTable.rows[i].οnclick=function(){clickfunction(this);last_tr_click.className="dg_tr_normal2";this.className="dg_tr_click";last_tr_click=this;};
objTable.rows[i].οnmοuseοver=function(){
  if(this.className!="dg_tr_click")
  {
  this.className="dg_tr_mouseover"
  }//end if
  };//end function
}//end for

}//end function
) ,(init_tr(this)), (this.className="dg")));
}

/*
初始化样式
*/
.dg
{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
vertical-align:middle;
padding:0px 3px;
border-collapse:collapse;
border: 1px solid #aaaaee;
width: 200px;
text-align: center;

}


/*
鼠标移上
*/
.dg_tr_mouseover,.dg_tr_mouseover a
{
color:#00FF00;
background-color: #ddffdd;
cursor:hand;
}
/*
标题行
*/
.dg_tr_head
{
margin:2px;
padding:2px;
font-weight: bold;
background-color: #0000ff;
color:#ffffff;
font-size:14px;
height:0;
word-break:keep-all;
}
/*
偶数行
*/
.dg_tr_normal2
{
background-color: #ffffff;
color:#000000;
cursor:hand;
}
/*
奇数行
*/
.dg_tr_normal
{
background-color: #f0f0f0;
color:#000000;
cursor:hand;
border-color:Purple:
}
/*
选中行
*/
.dg_tr_click
{
  background-color: #aaaaaa;
  color:Navy;
  cursor:default;
}

</style>
<! 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 >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312"   />
< title > 无标题文档 </ title >
</ head >< body >
< style >
.mytable 
{border-collapse:collapse;border:solid #6AA70B;border-width:0px 0 0 0px;width:50%;}
.mytable ul li 
{padding-top:5px;text-indent:2em;list-style:none;background:url(http://www.blueidea.com/img/common/new_flash.gif) 3px 50% no-repeat;border-bottom:#6AA70B 1px dotted ;font-family: "Verdana,宋体";font-size: 12px;color:#008000;text-align:left;height:25px;}
.mytable ul li.t1 
{background-color:#EFFEDD;}/* 第一行的背景色 */
.mytable ul li.t2
{background-color:#ffffff;}/* 第二行的背景色 */
.mytable ul li.t3 
{background-color:#D2FCA0;}/* 鼠标经过时的背景色 */
</ style >
< body  style =margin:0; >< br >< br >< br >
< div  align ="center" >
< div  class =mytable  id =tab >
< ul >
    
< li >< href ="#" > 这个是第1行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第2行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第3行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第4行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第5行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第6行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第7行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第8行的文字,大家看清楚了 </ a ></ li >
< li >< href ="#" > 这个是第9行的文字,大家看清楚了 </ a ></ li >
</ ul ></ div >< script  type ="text/javascript" >
<!--
var Ptr=document.getElementById("tab").getElementsByTagName("li");
function $() {
      
for (i=1;i<Ptr.length+1;i++
      Ptr[i
-1].className = (i%2>0)?"t1":"t2"
      }

}

window.onload
=$;
for(var i=0;i<Ptr.length;i++{
      Ptr
<i>.onmouseover=function(){
      
this.tmpClass=this.className;
      
this.className = "t3";    
      }
;
      Ptr[i].onmouseout
=function(){
      
this.className=this.tmpClass;
      }
;
}

//-->
</ script >
</ body >
</ html >
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值