【问题标题】:Keyboard navigation (up/down) link issue键盘导航(上/下)链接问题
【发布时间】:2019-05-27 05:25:15
【问题描述】:

我正在尝试让我的应用程序支持带有无序列表中链接的向上/向下箭头。

我正在复制https://jsfiddle.net/cse_tushar/5LM4R/,它几乎可以满足我的需要。但是,我需要能够按回车键并导航焦点所在的链接。

我的无序列表看起来像:

 <ul>
    <li><a href="#">First Link</a></li>
    <li><a href="#">Second Link</a></li>
    <li><a href="#">Third Link</a></li>
 </ul>

我的 jQuery 看起来像(目前仅用于向下箭头):

 $(document).keyup(function(e) {
   var focused_li, next;
   if (e.which === 40) { // Down arrow
      focused_li;
        if (focused_li) {
           alert("FOCUSED"); // NOT ALERTING
           focused_li.find("a").focusout();
           next = focused_li.next();
           if (next.length > 0) {
             return console.log("there is a next");
           } else {
             return console.log("there is no next");
           }
        } else {
           focused_li = $("ul li").eq(0);
           focused_li.find("a").focus()
        }
    } else if (e.which === 38) { // Up arrow 

    } else {

    }

  });

这是一个 JSFiddle:https://jsfiddle.net/p48fkw0v/

目前,它没有提醒我在哪里有 alert("FOCUSED"),我无法解决这个问题。

【问题讨论】:

  • 有一个if (focused_li),但当代码到达if 时,它还没有设置为任何值。你的意思是把它设置成什么吗?也许您打算将 focused_li 移到此函数之外,进入全局范围?

标签: javascript jquery html keyboard keyboard-shortcuts


【解决方案1】:

我真的不知道你想用“focused_li”做什么,但我想出了一个不同的解决方案。

这个循环遍历您的列表以检查哪个元素被聚焦并相应地聚焦下一个元素

新的JS代码:

$(document).keyup(function(e) {
if (e.which === 40) {
  focus = false
    links = $('a') //feel free to add more links it'll still work
  for (var i = 0; i < links.length; i++) {
    if (links[i] === (document.activeElement)) {
      focus = true;
        if (links[i+1] === undefined) {
      console.log("last element") //reached last link
      } else {
      links[i+1].focus(); //focuses next link if available
      }
    break;
    }
  }
  if (!focus) {
    links[0].focus() //if none of the links is focused, the first one gets focused
  }
} else if (e.which === 38) {
            //adapt the above code to links[i-1]
} else {

}

});

这是 JSfiddle:

https://jsfiddle.net/90jso3cq/

回答您关于输入触发链接的部分问题,只要您有一个有效的链接,它就会自动发生

编辑

代码获取类“链接”的所有元素,并且有时它们不是按顺序排列的

这是新的 HTML

<ul>
   <li><a id="link_1"class="link" href="#">First Link</a></li>
   <li><a id="link_2"class="link" href="#">Second Link</a></li>
   <li><a id="link_3"class="link" href="#">Third Link</a></li>
<ul>

和新的 JS

if (e.which === 40) {
  focus = false
    links = $('.link') //feel free to add more links itll still work
  for (var i = 0; i < links.length; i++) {
    if (links[i] === (document.activeElement)) {
      focus = true;
        if (links[i+1] === undefined) {
      console.log("last element") //reached last link
      } else {
      console.log(document.getElementById("link_" + (i + 2)))
      document.getElementById("link_" + (i + 2)).focus(); //focuses next link if available
      }
    break;
    }
  }
  if (!focus) {
    document.getElementById("link_1").focus() //if none of the links is focused, the first one gets focused
  }
} else if (e.which === 38) {
            //adapt the above code to links[i-1]
} else {

}

});

最后但并非最不重要的新小提琴

https://jsfiddle.net/8pdtsxjv/

【讨论】:

  • 谢谢,这可行,但是,出现了一个奇怪的问题。有时,这会跳过链接,而不是递增 1,而是随机数?
  • @coldicetea 你能试着解决这个问题吗,我似乎可以重新创建它......
  • 我已经更新了我的答案,现在应该可以正常使用了
猜你喜欢
  • 2011-09-03
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2023-02-06
  • 2020-10-18
  • 1970-01-01
相关资源
最近更新 更多