繁体   English   中英

按下键盘上的向左或向右箭头时,网页更改

[英]Web page change when left or right arrow in keyboard pushed

我想问一下如何在用户按下箭头键时更改网页,例如漫画/书籍网,如果我们要下一页,我们只需按下箭头键

对不起,我的英语,谢谢

您可以使用jQuery:

$(document).keydown(function(e) {
    if (e.which == 37) { 
       alert("left");
       e.preventDefault();
       return false;
    }

    if (e.which == 39) { 
       alert("right");
       e.preventDefault();
       return false;
    }
});

如果您希望使用jQuery框架,请查看JS / jQuery中的Binding方向键。

用普通的javascript,我将使用:

document.onkeydown = function (e) { 
  e = e || window.event; 
  var charCode = (e.charCode) ? e.charCode : e.keyCode;

  if (charCode == 37) {
        alert('left');
  }
  else if (charCode == 39) {
    alert('right');
  }
};

这是一个非jQuery选项:

document.onkeydown = arrowChecker;

function arrowChecker(e) {  
    e = e || window.event;
    if (e.keyCode == '37') { //left
        document.location.href = "http://stackoverflow.com/";
    }
    else if (e.keyCode == '39') { //right
       document.location.href = "http://google.com/";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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