简体   繁体   中英

Keyboard navigation (up/down) link issue

I'm trying to get my application to support up/down arrows with links in an unordered list.

I'm copying https://jsfiddle.net/cse_tushar/5LM4R/ , which does almost what I need. However, I need the ability to hit enter and navigate the link that is focused.

My unordered list looks like:

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

My jQuery looks like (only for the down arrow so far):

 $(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 {

    }

  });

Here's a JSFiddle: https://jsfiddle.net/p48fkw0v/

At the moment, it's not alerting where I have the alert("FOCUSED") and I can't get past this issue.

I don't really know what you were going for with 'focused_li' but I've come up with a different solution.

This one loops through your list to check which element is focused and focuses the next element accordingly

The new JS code:

$(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 {

}

});

here's the JSfiddle:

https://jsfiddle.net/90jso3cq/

to answer the part of your question about enter triggering the link, that happens automatically as long as you have a valid link

EDIT

The code gets all elements of class 'links' and sometimes they are not in order

here is the new 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>

and the new 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 {

}

});

and last but not least the new fiddle

https://jsfiddle.net/8pdtsxjv/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM