键盘上下控制Table 行选择效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        .actived {
            background-color:yellow;
        }
    </style>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    
    <script type="text/javascript">
        var index = 0;
        $(function () {
            $("body").keydown(function (e) {
                if (e.keyCode == 38) {
                    //上
                    if ($("#tb .actived").size() == 0) {
                        //默认第一个
                        index = 0;
                    } else {
                        index--;
                        if (index < 0) {
                            index = $("#tb tr").size() - 1;
                        }
                    }
                    $("#tb tr").eq(index).click();
                }
                if (e.keyCode == 40) {
                    //下
                    if ($("#tb .actived").size() == 0) {
                        //默认第一个
                        index = 0;
                    } else {
                        index++;
                        if (index > $("#tb tr").size() - 1) {
                            index = 0;
                        }
                    }
                    $("#tb tr").eq(index).click();
                }
                if (e.keyCode == 13) {
                    if ($("#tb .actived").size() > 0) {
                        //有选中的时候
                        alert("跳转地址:" + $("#tb tr").eq(index).attr("data-src"));
                        window.location.href = $("#tb tr").eq(index).attr("data-src");
                    }
                }
            });
            $("#tb tr").click(function(){
                $("#tb tr").removeClass("actived");
                $(this).addClass("actived");
                index = $(this).index();
            });
        });

    </script>
</head>
<body>
    
    <table id="tb">
        <tr data-src="http://www.baidu.com?v=1">
            <td>第1个</td>
        </tr>
        <tr data-src="http://www.baidu.com?v=2">
            <td>第2个</td>
        </tr>
        <tr data-src="http://www.baidu.com?v=3">
            <td>第3个</td>
        </tr>
        <tr data-src="http://www.baidu.com?v=4">
            <td>第4个</td>
        </tr>
    </table>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值