【发布时间】:2018-11-30 19:52:55
【问题描述】:
通过按 alt 键并单击 url,chrome 会进行下载。这是因为浏览器默认的 alt+click 操作。我怎样才能防止这种情况?它应该交换网址。
该代码适用于浏览器没有默认操作的其他键(例如 x == 88)。但是如何使用 alt 键呢?
//ALT-key recognition
document.onkeydown=function(){
var keyCode = event.keyCode;
if(keyCode == 18) //alt-key
{
console.log(keyCode);
document.getElementById("hist").setAttribute ('href', "history_by-date.php?wdate=all");
}
}
<div id="history">
<a href="history_by-date.php?wdate=2018&until=2017" id="hist" target="_self" class="a2">should be just a link - not a download (when alt is pressed)</a>
</div><!-- end history -->
skyline3000 的另一个用户有一个有趣的article 带有阻止代码,但我让它不起作用。感谢您的帮助。
evt.preventDefault();
evt.stopPropagation();
return false;
【问题讨论】:
标签: javascript google-chrome browser keyboard-shortcuts