jQuery表格行鼠标悬停变色

撰写:2020年1月14日

注意:不能保证三五年后,本博文对于新版的浏览器和jQuery仍然有效,请斟酌参考!

一、系统环境

  • Windows 10 64bit 1903
  • Chrome 79 64bit
  • jQuery 3.4.1

二、代码

2.1 创建样板html

使用VS Code创建一个包含表格的html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>鼠标悬停变色</title>
    <style>
        table{
            margin: 10px auto;
            border-collapse: collapse;
        }
        tr td{
            border: 1px solid black;
            margin: 5px;
            padding: 5px;
            height: 30px;
        }
    </style>
</head>
<body>
    <table id="DataGrid1">
        <tbody>
            <tr>
                <td>1</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>2</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>3</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>4</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>5</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

将上述代码保存为悬停变色.html,双击该文件,即可在浏览器中看到如下显示结果:

表格结果

2.2 引入jQuery

可以使用CDN引入jQuery,CDN的网址如下:https://www.bootcdn.cn/jquery/,打开该网址,复制最新的3.4.1到剪贴板,将其加入到html的head标签内。引入jQuery的代码如下:

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>

2.3 hover实现悬停变色

实现表格行悬停变色,可以采用jQuery中的hover事件。hover的语法如下:

$( selector ).hover( handlerIn, handlerOut )

其中selector为选择器,handerIn为鼠标进入时的回调函数,handerOut为鼠标离开时的回调函数。
悬停变色.html的head标签中新建一个script标签,其中代码如下:

$(document).ready(function(){
    $("table#DataGrid1 tr").hover(function () {
        $(this).css("background-color", "yellow");
    }, function () {
        $(this).css("background-color", "white");
    });
});

上面这部分代码实现了当鼠标进入时,表格行背景色变为黄色,鼠标离开时表格行背景色变为白色
效果如下:

鼠标悬停表格行变色.gif

2.4 完整代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>鼠标悬停变色</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <style>
        table{
            margin: 10px auto;
            border-collapse: collapse;
        }
        tr td{
            border: 1px solid black;
            margin: 5px;
            padding: 5px;
            height: 30px;
        }
        /* tr:hover{
            background-color: #dafdf3;
        } */
    </style>
    <script>
        $(document).ready(function(){
            $("table#DataGrid1 tr").hover(function () {
                $(this).css("background-color", "yellow");
            }, function () {
                $(this).css("background-color", "white");
            });
        });
    </script>
</head>
<body>
    <table id="DataGrid1">
        <tbody>
            <tr>
                <td>1</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>2</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>3</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>4</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
            <tr>
                <td>5</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
                <td>前端开发中经常使用到table表格</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章 入门 基本功能:访问和操作 dom 元素,控制页面样式,对页面的事件处理,与ajax完美结合,有丰富的插件...
    X_Arts阅读 1,066评论 0 2
  • 通过jQuery,您可以选取(查询,query)HTML元素,并对它们执行“操作”(actions)。 jQuer...
    枇杷树8824阅读 670评论 0 3
  • 看腻导航栏背景变色的hover效果,我们看一下比较特殊的hover效果。 分析 我们观察到,当鼠标悬停在导航栏的项...
    jdzhangxin阅读 3,390评论 0 3
  • JQuery简介 jQuery由美国人John Resig在2006年初创建,是继prototype之后又一个优秀...
    陈先森mansplain阅读 415评论 0 1
  • 陪伴是最长情的告白。 今天一天都在陪家人,上午开始开车带着妈妈、爱人和女儿去许昌购物,中午和妹妹汇合,买了...
    徐晓琳111阅读 144评论 0 0