用于网站键盘的Javascript导航 - 需要帮助

时间:2018-04-05 18:01:45

标签: css keyboard navigation keydown

我正在尝试仅使用键盘创建网站导航。 我在网上搜索了一些样本,但我没有取得什么进展。

没有我达到我的极限,应有的知识。我想获得所选li / href的值,以便我可以用enter打开该链接。 到目前为止,这是我的代码示例。



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Navigation</title>
  <style>
    li.selected {
      background: yellow
    }
    
    a:hover {
      color: blue
    }
  </style>
  <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
</head>

<body>
  <ul>
    <li><a id="link1" href="http://www.bing.de">First Link</a></li>
    <li> <a href="http://www.google.de">Second Link</a></li>
    <li> <a href="http://www.duckduckgo.com">Third Link</a></li>
  </ul>
  <script>
    var li = $('li');
    var liSelected;
    $(window).keydown(function(e) {
      if (e.which === 40) { //40=Pfeil nach unten
        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.which === 38) { //40=Pfeil nach oben
        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');
        }
      } else if (e.which === 13) { //13=Enter
        //missing code, how to open the selected href link from above
      }
    });
  </script>
</body>

</html>
&#13;
&#13;
&#13;

我期待得到一些提示。

谢谢,AxLED

3 个答案:

答案 0 :(得分:1)

你不需要为 Enter 实际添加事件,但是你走了:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Navigation</title>
  <style>
    li.selected {
      background: yellow
    }
    
    a:hover {
      color: blue
    }
  </style>
  <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
</head>

<body>
  <ul>
    <li><a id="link1" href="http://www.bing.de">Bing Link</a></li>
    <li> <a href="http://www.google.de">google Link</a></li>
    <li> <a href="http://www.duckduckgo.com">Duck Duck Link</a></li>
  </ul>
  <script>
    var li = $('li');
    var liSelected;
    $(window).keydown(function(e) {
      if (e.which === 40) { //40=Pfeil nach unten
        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.which === 38) { //40=Pfeil nach oben
        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');
        }
      } else if (e.which === 13) { //13=Enter
         window.open(liSelected.find("a").attr('href'));
      }
    });
  </script>
</body>

</html>

答案 1 :(得分:0)

添加:

//missing code, how to open the selected href link from above
liSelected.find("a")[0].click();

应该做的工作。 liSelected是正确的列表节点;可点击的锚标记是liSelected节点的子节点,可以通过find()找到。

请注意,如果未按下箭头键,则liSelected可能未定义,因此您需要为条件添加一个检查(例如else if(e.which === 13 && liSelected))。

答案 2 :(得分:0)

您可以在所选列表项的锚点上使用attr方法。

Click here 让笔帮助您

&#13;
&#13;
    var li = $('li');
    var liSelected;
    $(window).keydown(function(e) {
      if (e.which === 40) { //40=Pfeil nach unten
        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.which === 38) { //40=Pfeil nach oben
        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');
        }
      } else if (e.which === 13) { 
        window.location.href = $('.selected a').attr('href');
        
      }

      

      
      
      
    });
&#13;
   li.selected {
      background: yellow
    }
    
    a:hover {
      color: blue
    }
 
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<ul class="linksList">
    <li><a id="link1" href="https://www.bing.de">First Link</a></li>
    <li> <a href="https://www.bing.de">Second Link</a></li>
    <li> <a  href="https://www.bing.de">Third Link</a></li>
  </ul>
&#13;
&#13;
&#13;