【问题标题】:Disable focus outside the page禁用页面外的焦点
【发布时间】:2016-06-26 20:53:20
【问题描述】:

我正在制作一个基于网页的应用程序,它只有键盘(操纵杆)导航。我使用 tabindex,但我还需要禁用对地址栏、搜索栏或页面外任何其他内容的关注。

此应用只能在一台特定设备上运行,因此可以(实际上需要)禁用某些功能。

有可能吗?

【问题讨论】:

    标签: javascript jquery navigation focus tabindex


    【解决方案1】:

    这是一个很酷的问题。

    EDIT:添加了向后的[shift]+[tab]。

    试试这个脚本,(working Fiddle here):

    var firstInputObj;
    var lastInputObj;
    
    $("input").each(function(){
        if($(this).attr("tabIndex")=="1"){
            firstInputObj=$(this);
        }
        lastInputObj=$(this);
    
    });
    $(firstInputObj).focus();
    
    // tab (forward)
    $(lastInputObj).on("keydown",function(e){
        if(!e.shiftKey && e.which==9){
            e.preventDefault();
            $(firstInputObj).focus();
        }
    });
    
    // Shift tab (backward)
    $(firstInputObj).on("keydown",function(e){
        if(e.shiftKey && e.keyCode == 9) {
            e.preventDefault();
            $(lastInputObj).focus();
        }
    });
    

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多