Enter button works as tab

大兔子大兔子 提交于 2019-12-12 01:41:58

问题


I just have button in a page and nothing else

<button>hello</button>

When I open the page and press tab it comes to button but instead I want it in such a way that if I hit enter it should come to that button.

This is the code that I have written but it is not working

$(document).ready(function () {
$('.EntTab').on('keydown', function (e) {
    if (e.keyCode == 13) e.keyCode = 9;
    return false;
});
});

Need help in tackling this.


回答1:


Check this!

$(function() {
  $(document).keydown(function(e) {
    if (e.keyCode == 13) {
      $("button#hel").focus();
    } else if (e.keyCode == 9) {
      var e = jQuery.Event("keydown", {
        keyCode: 9
      });
      $(document).trigger(e);
    }
    return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="hel">hello</button>
<button>hello</button>
<button>hello</button>


来源:https://stackoverflow.com/questions/36398003/enter-button-works-as-tab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!