简体   繁体   中英

issue with choosing li elements using the arrow keys in javascript or jquery

I have been trying to do this forever now but i didnt know how. Basically i have this code below that generates li elements and append them to an already existing ul.

$(document.body).on("keyup", "", ".menuSearch", function (e) {
    if (e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40) {
        $(".searchResults").html("");
        var searchField = $(".menuSearch").val();
        var expression = new RegExp(searchField, "i");
        HidingShowingUlForSearch();
        $.each(finalJsonForMenu, function (key, value) {
            if (value.title.search(expression) != -1 || value.description.search(expression) != -1) {
                $("#searchResults").append(
                    '<li class="list-group-item mickyMouseResultLi" style=" border:none; min-width: 95%;">' +
                    '<a target="_blank" href="' + value.url + '"</a>' +
                    '<p>' +
                    value.title + '</p>' + '</li>');

                //+'<p style="color: #757373">' + value.description + '</p>' 
            }
        });
    }
});

this is the generated HTML by this code:

<ul class="list-group searchResults pre-scrollable mickyMouseUl" style="position: absolute; z-index: 13; border: 1px solid rgb(206, 206, 206); display: block;" id="searchResults">
  <li class="list-group-item mickyMouseResultLi" tabindex="1" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU/APIIT PAGOL</p>
    </a>
  </li>
  <li class="list-group-item mickyMouseResultLi" tabindex="2" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU B.Sc. Presentations</p>
    </a>
  </li>
  <li class="list-group-item mickyMouseResultLi" tabindex="3" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU Parking Guideline</p>
    </a>
  </li>
  <li class="list-group-item mickyMouseResultLi" tabindex="4" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU Documents</p>
    </a>
  </li>
  <li class="list-group-item mickyMouseResultLi" tabindex="5" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU Student Handbook</p>
    </a>
  </li>
  <li class="list-group-item mickyMouseResultLi" tabindex="6" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU B.Sc Presentations &amp; EOS Interview/LCM Schedules - Management Console</p>
    </a>
  </li>
  <li class="list-group-item mickyMouseResultLi" tabindex="7" style=" border:none; min-width: 95%;">
    <a target="_blank" href="testlink" <="" a="">
      <p>APU B.Sc Presentations &amp; EOS Interview/LCM Schedules</p>
    </a>
  </li>
</ul>

now thats great its working the way I want it too but then after the li generates i have to use the courser to point and click on them, while I need a way that i can just use the arrow keys with the enter button to access the newly generated li's. any suggestions?

First of all the generated link is wrong <a target="_blank" href="testlink" <="" a=""> . This <="" can't be a valid attribute.

Second point: I don't know how your code walk throught the li / a but I tried to make a complete working solution starting from your info.

I implemented some code from this answer: https://stackoverflow.com/a/8902976/4031083 . Obviously is edited for the context.

The new code added is this:

var liSelected;
$(window).keydown(function(e) {
        var li = $('li');
    if(e.keyCode === 40) {
        if(liSelected) {
            liSelected.removeClass('selected');
            next = liSelected.next();
            if(next.length > 0) {
                liSelected = next.addClass('selected');
            } else {
                liSelected = li.eq(0).addClass('selected');
            }
        } else {
            liSelected = li.eq(0).addClass('selected');
        }
    } else if(e.keyCode === 38) {
        if(liSelected) {
            liSelected.removeClass('selected');
            next = liSelected.prev();
            if(next.length > 0) {
                liSelected = next.addClass('selected');
            } else {
                liSelected = li.last().addClass('selected');
            }
        } else {
            liSelected = li.last().addClass('selected');
        }
    }
        liSelected.find('a').focus();
});

Here the complete JSFiddle.

Hope this help you.

So I have figured it out using javascript and the steps as follows:

1- you must get the ul and get its first child and add an id to it:

var list = document.getElementById('searchResults'); // targets the<ul>
var first = list.firstChild; // targets the first <li>
first.setAttribute('id', 'selected');

2- Then you can use Document.boy on key up and implement the below switch case:

 switch (e.keyCode) {
            case 38: // if the UP key is pressed
                var ulSelected = document.getElementById('searchResults');

                var selected = document.getElementById('selected');
                if (selected.previousSibling == null) {
                    break;
                }
                var newSelected = selected.previousSibling;
                selected.removeAttribute('id');
                newSelected.setAttribute('id', 'selected');
                var searchBar = document.getElementById('menuSearch');
                searchBar.value = selected.firstChild.innerText;
                MickyAutoScroll(selected, ulSelected)
                break;
            case 40: // if the DOWN key is pressed
                var ulSelected = document.getElementById('searchResults');
                var selected = document.getElementById('selected');
                if (selected.nextSibling == null) {
                    break;
                }
                var newSelected = selected.nextSibling;
                selected.removeAttribute('id');
                newSelected.setAttribute('id', 'selected');
                var searchBar = document.getElementById('menuSearch');
                searchBar.value = selected.firstChild.innerText;
                MickyAutoScroll(selected, ulSelected)
                break;
            case 13:
                var link = document.getElementById("selected");
                link.firstChild.click();
                break;
            case 27:
                var searchBar = document.getElementById("menuSearch");
                searchBar.value = null;
                break;
        }

this basically will move up, down and access the link in the li using the enter button

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