jQuery实现Table表格隔行变色及高亮显示当前选择行效果示例

所属分类: 网络编程 / JavaScript 阅读数: 884
收藏 0 赞 0 分享

本文实例讲述了jQuery实现Table表格隔行变色及高亮显示当前选择行效果。分享给大家供大家参考,具体如下:

最近客户要求高亮选择列表的功能,于是顺便做了个,作为记录。

前台代码:

<!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=utf-8" />
  <title>JQuer的鼠标悬浮,鼠标高亮效果</title>
  <style type="text/css">
  #header
  {
    background-color:#00ffff;
    text-align:center;
  }
  .style1
  {
    text-align: right;
  }
  .style2
  {
    text-align: center;
  }
  </style>
  <link href="tables.css" rel="external nofollow" rel="stylesheet" type="text/css" />
  <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(document).ready(function() {
    doChangeColorOfRow("#tableThis tr:even:not(#header)", "#tableThis tr:odd:not(#header)");
    });
    function doChangeColorOfRow(evenTR, oddTR) {
    $(evenTR).each(function() {
      $(this).css("background-color", "#F0F8FF").bind("mouseover", function() {
        if ($(this).css("background-color") != "#ffff00") {
          $(this).css("background-color", "#D8FAD8");
        }
      }).bind("mouseout", function() {
        if ($(this).css("background-color") != "#ffff00") {
          $(this).css("background-color", "#F0F8FF");
        }
      }).bind("click", function() {
        $(evenTR).each(function() {
          if ($(this).css("background-color") == "#ffff00") {
            $(this).css("background-color", "#F0F8FF");
          }
        });
        $(oddTR).each(function() {
          if ($(this).css("background-color") == "#ffff00") {
            $(this).css("background-color", "#ffffff");
          }
        });
        $(this).css("background-color", "#ffff00");
      });
    });
    $(oddTR).each(function() {
      $(this).css("background-color", "#ffffff").bind("mouseover", function() {
        if ($(this).css("background-color") != "#ffff00") {
          $(this).css("background-color", "#D8FAD8");
        }
      }).bind("mouseout", function() {
        if ($(this).css("background-color") != "#ffff00") {
          $(this).css("background-color", "#ffffff");
        }
      }).bind("click", function() {
        $(evenTR).each(function() {
          if ($(this).css("background-color") == "#ffff00") {
            $(this).css("background-color", "#F0F8FF");
          }
        });
        $(oddTR).each(function() {
          if ($(this).css("background-color") == "#ffff00") {
            $(this).css("background-color", "#ffffff");
          }
        });
        $(this).css("background-color", "#ffff00");
      });
    });
  }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <table style="width: 100%;" cellpadding="0" cellspacing="0" id="tableThis">
    <tr id="header">
      <td>纳税人</td>
      <td>
        &nbsp;
        增值税</td>
      <td>
        &nbsp;
        消费税</td>
      <td>
        &nbsp;
        营业税</td>
        <td>小规模纳税人</td>
        <td>增值税小规模纳税人</td>
    </tr>
    <tr>
    <td class="style2">张三</td>
      <td class="style1">
        123423432.12</td>
      <td class="style1">
        &nbsp;
        32445345.13</td>
      <td class="style1">
        &nbsp;
        345564.25</td>
              <td class="style1">567657567.78</td>
        <td class="style1">3454353453.90</td>
    </tr>
    <tr>
    <td class="style2">李四</td>
      <td class="style1">
        &nbsp;
        34435345.34</td>
      <td class="style1">
        &nbsp;
        456546</td>
      <td class="style1">
        &nbsp;
        675675</td>
              <td class="style1">678879789</td>
        <td class="style1">34534534.0</td>
    </tr>
     <tr>
    <td class="style2">王五</td>
      <td class="style1">
        &nbsp;
        23424</td>
      <td class="style1">
        &nbsp;
        6546</td>
      <td class="style1">
        &nbsp;
        67868</td>
              <td class="style1">980890</td>
        <td class="style1">45345</td>
    </tr>
     <tr>
    <td class="style2">刘六</td>
      <td class="style1">
        &nbsp;
        234234</td>
      <td class="style1">
        &nbsp;
        123123</td>
      <td class="style1">
        &nbsp;
        324234</td>
              <td class="style1">342423</td>
        <td class="style1">345345</td>
    </tr>
     <tr>
    <td class="style2">赵七</td>
      <td class="style1">
        &nbsp;
        345345</td>
      <td class="style1">
        &nbsp;
        546546</td>
      <td class="style1">
        &nbsp;
        567567</td>
              <td class="style1">67867867</td>
        <td class="style1">67867</td>
    </tr>
     <tr>
    <td class="style2">王八</td>
      <td class="style1">
        &nbsp;
        345354</td>
      <td class="style1">
        &nbsp;
        345345</td>
      <td class="style1">
        &nbsp;
        5654</td>
              <td class="style1">567658678</td>
        <td class="style1">879879789</td>
    </tr>
     <tr>
    <td class="style2">李九</td>
      <td class="style1">
        &nbsp;
        34535</td>
      <td class="style1">
        &nbsp;
        4534</td>
      <td class="style1">
        &nbsp;
        756765</td>
              <td class="style1">867867</td>
        <td class="style1">897987987</td>
    </tr>
     <tr>
    <td class="style2">周十</td>
      <td class="style1">
        &nbsp;
        456434534</td>
      <td class="style1">
        546456&nbsp;
      </td>
      <td class="style1">
        &nbsp;
        5675756</td>
              <td class="style1">67867867</td>
        <td class="style1">8797987</td>
    </tr>
     </table>
  </form>
</body>
</html>

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery表格(table)操作技巧汇总》、《jQuery切换特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常用插件及用法总结》、《jQuery常见经典特效汇总》及《jquery选择器用法总结

希望本文所述对大家jQuery程序设计有所帮助。

更多精彩内容其他人还在看

BootStrap数据表格实例代码

本文通过实例代码给大家分享了BootStrap数据表格的相关知识,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

基于vue的短信验证码倒计时demo

这篇文章主要介绍了基于vue的短信验证码倒计时demo,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解React Native开源时间日期选择器组件(react-native-datetime)

本篇文章主要介绍了详解React Native开源时间日期选择器组件(react-native-datetime),具有一定的参考价值,有兴趣的可以了解一下
收藏 0 赞 0 分享

JS库particles.js创建超炫背景粒子插件(附源码下载)

particles.js用于创建粒子的轻量级 JavaScript 库。使用方法非常简单,代码也很容易实现,下面通过本文给大家分享JS库particles.js创建超炫背景粒子插件附源码下载,需要的朋友参考下吧
收藏 0 赞 0 分享

JS库之Waypoints的用法详解

waypoints的功能非常强大,一款用于捕获各种滚动事件的插件,下面跟随脚本之家小编一起学习JS库之Waypoints的用法吧
收藏 0 赞 0 分享

强大的JavaScript响应式图表Chartist.js的使用

本篇文章主要介绍了强大的JavaScript响应式图表Chartist.js的使用,具有一定的参考价值,有兴趣的可以了解一下
收藏 0 赞 0 分享

详解wow.js中各种特效对应的类名

本篇文章主要介绍了wow.js中各种特效对应的类名 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

JS库之Highlight.js的用法详解

highlight.js是一款轻量级的Web代码语法高亮库。下面通过实例代码给大家分享JS库之Highlight.js的用法详解,感兴趣的朋友跟随脚本之家小编一起学习吧
收藏 0 赞 0 分享

详解动画插件wow.js的使用方法

本篇文章主要介绍了动画插件wow.js的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

JS库 Highlightjs 添加代码行号的实现代码

Highlightjs是一款优秀的代码高亮Js组件,可以很方便地对各种语言编写的代码添加语法高亮样式。本文重点给大家介绍Highlightjs 添加代码行号的实现代码,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多