JQuery行点击

本文介绍了一种使用JQuery实现的行点击效果,当鼠标悬停在表格的行上时,光标会变为手指形状,并且点击行时,被点击的行会高亮显示黄色背景,同时取消其他行的高亮状态。此外,还实现了输入框获得和失去焦点时的样式变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!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>
    <title>JQuery 行点击</title>
    <style type="text/css">
    .yelBG{ background-color:Yellow;}
    </style>
    <script src="js/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            //页面html代码解析完后,为每个行添加 鼠标移入事件 方法
            $("#t1 tr").mouseover(function () {
                $(this).css("cursor", "pointer"); //将鼠标改成 手型
            }).click(function () {//(链式操作)同时再为每个行添加 点击事件 方法
                //为当前点击的行 添加样式表 yelBG,然后为它的兄弟元素移除 yelBG 样式
                $(this).addClass("yelBG").siblings().removeClass("yelBG");

                var nowIndex = $("#t1 tr").index($(this)); //获得当前选中行的下标
                alert(nowIndex);
            });
            //获得焦点时添加样式
            $("input").focus(function () {
                $(this).addClass("yelBG");
            }).blur(function () {//(链式操作)失去焦点时 移除样式
                $(this).removeClass("yelBG");
            });
        })
    </script>
</head>
<body >
    <table id="t1" style="width: 100%;">
        <tr><td>1</td><td>2</td></tr>
        <tr><td>1</td><td>2</td></tr>
        <tr><td>1</td><td>2</td></tr>
        <tr><td>1</td><td>2</td></tr>
        <tr><td>1</td><td>2</td></tr>
    </table>
    <input type="text" /><input type="text" />
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值