Javascript 将箭头键导航添加到页面中带有变量的站点

Javascript 将箭头键导航添加到页面中带有变量的站点,javascript,navigation,navigationbar,Javascript,Navigation,Navigationbar,我想添加此代码,以允许通过带有左右箭头的网站进行导航。是否有任何方法可以从该页面上链接的图像中指定window.location变量?我试图将页面上用于导航的左箭头和右箭头分配给键盘上的左箭头和右箭头 img src="leftarrow.png" = previous page img src="rightarrow.png" = next page 要使用的代码:(其他代码也可以) 假设图像包装在锚定标记中(否则它将如何工作?),您可以执行以下操作: if (keycode == 37)

我想添加此代码,以允许通过带有左右箭头的网站进行导航。是否有任何方法可以从该页面上链接的图像中指定window.location变量?我试图将页面上用于导航的左箭头和右箭头分配给键盘上的左箭头和右箭头

img src="leftarrow.png" = previous page
img src="rightarrow.png" = next page
要使用的代码:(其他代码也可以)


假设图像包装在锚定标记中(否则它将如何工作?),您可以执行以下操作:

if (keycode == 37) {
    img = document.querySelector("img[src='leftarrow.png']");
    window.location = img.parentElement.href;
    return false;
} else if (keycode == 39) {
    img = document.querySelector("img[src='rightarrow.png']");
    window.location = img.parentElement.href;
    return false;
}

我们正在寻找合适的图像/导航链接,并从其锚容器中获取url。

是的,它包装在锚标记中。我会试试的谢谢Jaime。你能通过把信息放在上面的代码中来简化你的答案吗?我到处寻找如何让它在没有运气的情况下工作。谢谢。刚刚添加了一段代码,实现了解决方案。
if (keycode == 37) {
    img = document.querySelector("img[src='leftarrow.png']");
    window.location = img.parentElement.href;
    return false;
} else if (keycode == 39) {
    img = document.querySelector("img[src='rightarrow.png']");
    window.location = img.parentElement.href;
    return false;
}