我是javascript的新手,我尝试在我的网站上添加一个脚本,允许用户使用键盘导航到每个页面。到目前为止,我还没有能够让它发挥作用。这就是我所拥有的:
function addListener(element, type, response) {
if (element.addEventListener) {
element.addEventListener(type, response, false);
}
else if (element.attachEvent) {
element.attachEvent("on" + type, response);
}
}
addListener(window, "keypress", function(key) {
// do this stuff when a key is pressed:
key = key || window.event;
var theKey = key.which || key.keyCode;
switch (theKey) {
// if they hit a
case 65 :
window.location.href = "/about.html";
break;
// if they press r
case 82 :
window.location.href = "/resources.html";
break;
// if they hit l
case 76 :
window.location.href = "/links.html";
break;
// if they press h
case 72 :
window.location.href = "/index.html";
break;
}
});