首页 > PHP > 用jQuery实现表格颜色的交替显示

用jQuery实现表格颜色的交替显示

2009年04月10号
查看评论 发表评论 807次浏览

为了能够更清晰的显示表格中的数据,使用双色表格来显示数据的例子已经屡见不鲜了。我们通常都是使用JavaScript来实现这种效果,可是你知道jQuery中该如何做吗?用jQuery的语句重复JavaScript实现方法吗?不,其实jQuery已经为我们准备好了一切。

其实仅仅三句jQuery代码就实现了上面的效果,是不是很酷?下面我详细的讲一讲如何用jQuery来实现它:

首先建立表格的结构,别忘了给表格定义一个class,我这里定义的class名为truecolor。

下面是CSS部分,为表格的不同状态定义样式。

.truecolor {border:1px solid #333;text-align:center;} /* 非关键部分:给表格加个边框 */
.truecolor th {background-color:#333; color:#FFF;} /* 非关键部分:给表头定义样式 */
.color1 {background-color:#DDD; color:#333;} /* 关键部分:定义tr的第一种样式 */
.color2 {background-color:#EEE; color:#333;} /* 关键部分:定义tr的第二种样式 */
.color3 {background-color:#666; color:#FFF;} /* 关键部分:定义tr在鼠标悬浮状态的样式 */

现在样式定义好了,那么怎么把样式应用到表格上呢?下面进入最关键的jQuery部分。
/*
* 请确保你已经在之前引入了jQuery的js文件
* 我这里是使用Wordpress自带的jQuery
* 为避免框架的冲突,我在这里使用jQuery()而不是$()
*/
jQuery(document).ready(function(){
jQuery(“.truecolor tr:odd”).addClass(“color1″);
// 选择class为.truecolor的对象中的单数行,为该行添加.color1样式
jQuery(“.truecolor tr:even”).addClass(“color2″);
// 选择class为.truecolor的对象中的双数行,为该行添加.color2样式
jQuery(“.truecolor tr”).hover(function(){
jQuery(this).addClass(“color3″)
},function(){
jQuery(this).removeClass(“color3″)
});
// 格式为:jQuery选取对象.hover(鼠标移到对象触发的函数,鼠标移出对象触发的函数);
// 鼠标over时,添加.color3样式;鼠标out时,移除.color3样式
});

OK,现在已经大功告成了,快去试试效果吧!

类别PHP 标签
  1. Fake LV Sunglasses
    发表于 2010年06月18号 11时15分18秒 | 1楼

    Thanks,you are the greatest.

  2. Discount Designer Gucci Handbags
    发表于 2010年06月23号 08时25分35秒 | 2楼

    You wrote so many true words about signs, reading them and following them, that I too have alredy experienced some times.

  3. MBT Chapa Shoes
    发表于 2010年06月25号 12时44分19秒 | 3楼

    this year is G.O.O.D

  4. kobe shoes
    发表于 2010年06月29号 10时23分59秒 | 4楼

    yesterday, i saw this blog and read it! and there, so many realizations. thanks so much for enlightenment.

  5. paul smith shop
    发表于 2010年06月30号 08时36分59秒 | 5楼

    Great post, and so very true.

  6. herve leger dress
    发表于 2010年07月09号 15时56分30秒 | 6楼

    Great post, we often forget such a basic truth.

  7. prada wallets
    发表于 2010年07月19号 17时01分40秒 | 7楼

    I am especially impressed with your post.

  8. timberland custom booots
    发表于 2010年07月20号 17时31分42秒 | 8楼

    It was a great story…I was with you, while reading it.Very strong and discriptive!

  1. 目前没有通告
你必需 登陆 才能发表评论.