【发布时间】:2016-03-18 09:53:44
【问题描述】:
我想使用箭头(37 - 左箭头,39 - 右箭头)在页面之间切换。下面的代码可以在 Firefox、Chrome 和 Internet Explorer 上正常工作。
在浏览器中单击“返回”(返回浏览历史记录)按钮后,该解决方案不适用于 Microsoft Edge。有人知道怎么解决吗?
<script type="text/javascript">
window.addEventListener("keydown", checkKeyPressed, false);
function checkKeyPressed(event) {
var x = event.which || event.keyCode;
if (x == 37) { window.location.href = "page1.html";}
if (x == 39) { window.location.href = "page2.html";}
};
</script>
【问题讨论】:
-
page1.html 和 page2.html 中都有这个脚本块吗?
-
点击后退按钮后焦点是什么?如果不是
window(即后退按钮或其他某些 Edge 组件具有焦点),那么您的侦听器将不会收到该事件。 -
@Frode 我不认为 page1.html 或 page2.html 会阻止脚本,因为从其他页面以及外部站点返回时也会出现同样的问题。
-
@Paul 你知道如何修改这个脚本来获得窗口的焦点并使其工作。是否涉及放寡妇。焦点()某处?如果是,那么在哪里?