JQuery案例01-隔行换色功能+鼠标悬停变色

1.功能演示

2.HTML代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>隔行换色功能</title>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>商品名称</th>
          <th>产地</th>
          <th>厂商</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>爱美电视机</td>
          <td>福州</td>
          <td>爱美电子</td>
        </tr>
        <tr>
          <td>爱美电视机</td>
          <td>福州</td>
          <td>爱美电子</td>
        </tr>
        <tr>
          <td>爱美电视机</td>
          <td>福州</td>
          <td>爱美电子</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

3.CSS代码

       table{
            border: 0;  
            /* 当设置为collapse时,相邻单元格的边框会合并为一条,使表格看起来更加紧凑和整洁 */
            border-collapse: collapse;
        }
        td{
            font: bold 12px/17px Arial;
            padding: 2px;
            width: 100px;
        }
        th{
          /* 前面12px是定义字体大小,后面17px是定义行高,行高属性在字体属性后面用/分隔。 */
            font: bold 12px/17px Arial;
            text-align: left;
            padding: 4px;
            border-bottom: 1px solid #333;
        }
        .even{
            background-color: #ffc;
        }
        .odd{
            background-color: #cef;
        } 
       .light{
            background:#00A1DA;
        }

4.jQuery代码

引用jQuery在线链接

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

代码部分
 // 添加class
        $("tbody tr:even").addClass("even");
        $("tbody tr:odd").addClass("odd");
        // .hover 当鼠标移到到该标签会显示功能或者样式的事件
        $("tbody tr").hover(
            function () {
                $(this).addClass("light")
            },
            function () {
               $(this).removeClass("light")
            }
        );

5.整体代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>隔行换色功能</title>
    <!-- 引用jquery的在线链接 -->
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style>
        table{
            border: 0;  
            /* 当设置为collapse时,相邻单元格的边框会合并为一条,使表格看起来更加紧凑和整洁 */
            border-collapse: collapse;
        }
        td{
            font: bold 12px/17px Arial;
            padding: 2px;
            width: 100px;
        }
        th{
          /* 前面12px是定义字体大小,后面17px是定义行高,行高属性在字体属性后面用/分隔。 */
            font: bold 12px/17px Arial;
            text-align: left;
            padding: 4px;
            border-bottom: 1px solid #333;
        }
        .even{
            background-color: #ffc;
        }
        .odd{
            background-color: #cef;
        } 
       .light{
            background:#00A1DA;
        }
    </style>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>商品名称</th>
          <th>产地</th>
          <th>厂商</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>爱美电视机</td>
          <td>福州</td>
          <td>爱美电子</td>
        </tr>
        <tr>
          <td>爱美电视机</td>
          <td>福州</td>
          <td>爱美电子</td>
        </tr>
        <tr>
          <td>爱美电视机</td>
          <td>福州</td>
          <td>爱美电子</td>
        </tr>
      </tbody>
    </table>
  </body>
  <script>
        // 添加class
        $("tbody tr:even").addClass("even");
        $("tbody tr:odd").addClass("odd");
        // .hover 当鼠标移到到该标签会显示功能或者样式的事件
        $("tbody tr").hover(
            function () {
                $(this).addClass("light")
            },
            function () {
               $(this).removeClass("light")
            }
        );
  </script>
</html>

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值