当我使用箭头键时,如何打开侧面菜单?右箭头键打开它左箭头键关闭它。
以下是我的菜单打开和关闭方式的代码:
<script>
function toggle() {
if( document.getElementById("hidethis").style.display=='none' ){
document.getElementById("hidethis").style.display = '';
}else{
document.getElementById("hidethis").style.display = 'none';
}
</script>
我已经调查了这个主题,但是如果你可以告诉我或指向正确的方向,那么我就无法找到与我直接相关的任何内容。我相信你可以用某种keydown函数做到这一点,但不确定如何。
答案 0 :(得分:3)
document.addEventListener("keydown", function (e) {
if (e.keyCode === 39)
document.getElementById("hidethis").style.display = '';
if (e.keyCode === 37)
document.getElementById("hidethis").style.display = 'none';
}, false);
应该这样做。
答案 1 :(得分:2)
以下是Javascript代码,用于说明是否按下了右箭头或左箭头:
<!DOCTYPE html>
<html>
<head>
<script>
var thisKey;
document.onkeydown = keyHit;
var ltArrow = 37;
var rtArrow = 39;
function keyHit(evt) {
if(evt) {
thisKey = evt.which;
} else {
thisKey = window.event.keyCode;
}
if (thisKey == rtArrow) { //Right arrow key is pressed
document.getElementById("hidethis").style.display = '';
} else if (thisKey == ltArrow) { //Left arrow key is pressed
document.getElementById("hidethis").style.display = 'none';
}
}
</script>
</head>
<body>
</body>
</html>
我希望这有帮助!
答案 2 :(得分:0)
在jquery网页上尝试jquery keycode av 在普通的javascript中它的char