jQuery焦点到表 [英] jquery focus to table

查看:87
本文介绍了jQuery焦点到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载时将焦点放在表格的第一行中.我正在使用它进行导航 在键盘上的表中.如何使焦点对准,有时此键盘导航也无法正常工作 http://jsfiddle.net/hKZqS/8/使用键盘时,这是在去除斑马条纹

I want to bring focus to first row in a table when a page loads.I am using this for navigation in a table using key board.How do i bring focus and some times this keyboard navigation is also not working http://jsfiddle.net/hKZqS/8/ When using key board this is removing zebra stripes

推荐答案

尝试以下jQuery代码:

Try the following jQuery code:

$(document).ready(function(){
    $("#myTable tbody tr").first().addClass("ui-selected");
});

// Do the clever keypress stuff
$(document).keyup( function(e)
{
    switch(e.which)
    {
        // user presses the "a" key
        case 38:
            if(!e.ctrlKey) {
                var selected = $("tr.ui-selected").first();
                if (selected.prev().html() != null) {
                    selected.prev().addClass("ui-selected");
                    selected.removeClass("ui-selected");
                }
            }
            break;    
        // user presses the "s" key
        case 40:  
            if(!e.ctrlKey) {
                var selected = $("tr.ui-selected").first();
                if (selected.next().html() != null) {
                    selected.next().addClass("ui-selected");
                    selected.removeClass("ui-selected");
                }
            }
            break;
    }
});

我对其进行了测试,它似乎可以在jsFiddle的修改版本上运行:请参阅示例

I tested it out and it seems to be working on a modified version of your jsFiddle: See Sample

这篇关于jQuery焦点到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆