Keydown而不是点击按钮

时间:2016-10-08 16:13:48

标签: javascript jquery button keydown

按下回车键时尝试刷新页面,而不是默认点击按钮。我假设需要一个事件监听器而不确定如何调用该按钮。

  $(document).ready(function() {
function refresh (e) {
    if(e.keyCode === 13){

    }
  }
  $('body').on('keydown', refresh);
    })
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="script.js"></script>
</head>
<body>
  <div class='win'>
  <button><a href="index.html" style="text-decoration:none">Play Again</a></button>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

当您点击其键值为 13

ENTER时,这应该可以刷新页面
$(document).keypress(function(e) {
    if(e.which == 13) {
        location.reload();
    }
});