jQuery:表格的奇偶行变色

<html>
  <head>
    <title>表格变色</title>
    <style type="text/css">
        .mouseOver { background-color: #f6d7d7; }
        .odd { background-color: #f9fceb;}
        .even { background-color: #d4f1fb;}
        .first {background-color: #ffffff;}
    </style>
   <script src="https://code.jquery.com/jquery-3.6.0.js" type="text/javascript" ></script>
   <script type="text/javascript">
           $(function(){
                $("tr:odd").addClass("odd");        //加奇行样式
                $("tr:even").addClass("even");        //加偶行样式
                $("tr:first").addClass("first");    //为第一行加样式
                //为行元素加上鼠标移入和移出事件
				$("tr:gt(0)").mouseover(function() {
				    currClass=$(this).attr("class");
					$(this).attr("class","mouseOver");
				}).mouseout(function() {
					$(this).attr("class",currClass);
                });
           })
   </script>
  </head>
  <body>
      <table border="1">
          <tr><td>姓名</td><td>年龄</td></tr>
          <tr><td>王华</td><td>18</td></tr>
          <tr><td>刘云</td><td>19</td></tr>
          <tr><td>刘亮</td><td>15</td></tr>
          <tr><td>叶子</td><td>17</td></tr>
          <tr><td>刘梦</td><td>20</td></tr>
      </table>
  </body>
</html>
<html>
  <head>
    <title>表格变色</title>
    <style type="text/css">
        .mouseOver { background-color: #f6d7d7!important; }
        .odd { background-color: #f9fceb;}
        .even { background-color: #d4f1fb;}
        .first {background-color: #ffffff;}
    </style>
   <script src="https://code.jquery.com/jquery-3.6.0.js" type="text/javascript" ></script>
   <script type="text/javascript">
           $(function(){
                $("tr:odd").addClass("odd");        //加奇行样式
                $("tr:even").addClass("even");        //加偶行样式
                $("tr:first").addClass("first");    //为第一行加样式
                //为行元素加上鼠标移入和移出事件
				$("tr:gt(0)").mouseover(function() {
					$(this).addClass("mouseOver");
				}).mouseout(function() {
					$(this).removeClass("mouseOver");
                });
           })
   </script>
  </head>
  <body>
      <table border="1">
          <tr><td>姓名</td><td>年龄</td></tr>
          <tr><td>王华</td><td>18</td></tr>
          <tr><td>刘云</td><td>19</td></tr>
          <tr><td>刘亮</td><td>15</td></tr>
          <tr><td>叶子</td><td>17</td></tr>
          <tr><td>刘梦</td><td>20</td></tr>
      </table>
  </body>
</html>

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值