通过按alt键并点击url,chrome下载.这是因为浏览器默认的alt+click操作。我怎么才能防止这种情况?它应该交换网址。
代码与其他键(例如x == 88)一起工作,浏览器没有默认操作。但怎么处理阿尔特钥匙呢?
//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;
发布于 2018-11-30 20:38:14
我认为您需要侦听链接、单击事件和检查event.altKey属性,而不是听按键。我不是100%,如果这是防弹与alt点击下载,但它似乎是有效的。注意,只有在正常的href、没有javascript或单击事件的情况下,使用this.click()才能调用链接。否则,还有其他更复杂的方法来完成这部分工作。
document.querySelectorAll('a').forEach(a => {
a.addEventListener('click', function (e) {
if (e.altKey) {
e.preventDefault(); // no download
this.href = 'history_by-date.php?watchdate=all';
this.click(); // fake a normal click
}
});
});<a href='http://example.com'>alt-click me</a>
发布于 2018-11-30 20:38:35
您所要求的是更改用户浏览器的默认操作。这是不可能的浏览器中的默认操作不会在HTML中运行,因此无法检测到。
https://stackoverflow.com/questions/53564044
复制相似问题