简体   繁体   English

Javascript - 如何使用键盘箭头键更改焦点在列表中的链接

[英]Javascript - How to change focus on links in a list with keyboard arrow keys

I'm able to change focus when the links are not wrapped in other elements. 当链接没有包含在其他元素中时,我可以更改焦点。

This works: 这有效:

HTML HTML

<a id="first" href="#" class='move'>Link</a>
<a href="#" class='move'>Link</a>
<a href="#" class='move'>Link</a>

JS (with jQuery) JS(使用jQuery)

$(document).keydown(
    function(e)
    {    
        // Down key
        if (e.keyCode == 40) {      
            $(".move:focus").next().focus();       
        }

        // Up key
        if (e.keyCode == 38) {      
            $(".move:focus").prev().focus();       
        }
    }
);

Demo Fiddle 演示小提琴

But how do I achieve the same thing when the links are inside a list for example? 但是,当链接在列表中时,如何实现相同的功能呢? Like this 像这样

<ul>
    <li>
        <a id="first" href="#" class='move'>Link</a>
    </li>
    <li>
        <a href="#" class='move'>Link</a>
    </li>
    <li>
        <a href="#" class='move'>Link</a>
    </li>
</ul> 

You can use .closest() to find the parent element then use .next() to get the next li, then use .find() to get the next .move 您可以使用.closest()查找父元素,然后使用.next()获取下一个li,然后使用.find()获取下一个.move

    if (e.keyCode == 40) {      
        $(".move:focus").closest('li').next().find('a.move').focus();   
    }

    // Up key
    if (e.keyCode == 38) {      
        $(".move:focus").closest('li').prev().find('a.move').focus();   
    }

DEMO DEMO

if (e.keyCode == 40) {      
  $(".move:focus").parent().next().find('a').focus();   
}
if (e.keyCode == 38) {      
  $(".move:focus").parent().prev().find('a').focus();   
}

If you happen to want your focus to cycle when reaching the end of the list, you can do something like this: 如果您碰巧希望您的焦点在到达列表末尾时循环,您可以执行以下操作:

var $li = $('li'),

$move = $(".move").click(function () {
    this.focus();
});

$(document).keydown(function(e) {
    if (e.keyCode == 40 || e.keyCode == 38) {
        var inc = e.keyCode == 40 ? 1 : -1,
            move = $move.filter(":focus").parent('li').index() + inc;
        $li.eq(move % $li.length).find('.move').focus();
    }
});

$move.filter(':first').focus();

Demo: http://jsfiddle.net/WWQPR/5/ 演示: http//jsfiddle.net/WWQPR/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 使用键盘箭头键将焦点从输入转移到列表项链接,与 TAB 键相同 - Javascript to shift focus from input to list item links with keyboard arrow keys, the same as TAB key 如何在 JavaScript 中分配箭头和输入键盘键? - How to assign arrow and enter keyboard keys in JavaScript? 在 JavaScript 中使用箭头键移动焦点 - Shift focus with arrow keys in JavaScript 如何使用带有蓝牙键盘的iPad在JavaScript上检测箭头键 - How to detect arrow keys using JavaScript on iPad with Bluetooth keyboard 使用箭头键更改按钮上的焦点 - Use arrow keys to change focus on buttons 箭头键更改图片Javascript - Arrow keys change pics Javascript 如何获取键盘上的箭头键以触发博客中的导航(上一页/下一页)链接 - how to get the arrow keys on the keyboard to trigger navigation (previous/next page) links within a blog 如何在javascript中启用箭头键 - how to enable arrow keys in javascript 如何向右移动光标焦点。 在 JavaScript 中使用箭头键向左、向上和向下 - How to move cursor focus right. left , up and down using arrow keys in JavaScript Svelte:按向上箭头和向下箭头键时,如何将焦点设置到列表项中的上一个/下一个元素? - Svelte: How can I set the focus to the previous/next element in the list item when pressing UP arrow and DOWN arrow keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM