我试图拦截并阻止所有默认事件。任何人都可以指向文档/真理的源头来证明这是不可能的,例如:cmd
+ r
(Mac)(或Windows的ctrl
+ r
)
<html>
<head>
</head>
<body>
<div id="debug">
Debug:
</div>
<script>
document.addEventListener('keypress', (event) => {
const otherKeys = []
if (event.ctrlKey) {
otherKeys.push("CTRL")
}
if (event.shiftKey) {
otherKeys.push("SHIFT")
}
if (event.altKey) {
otherKeys.push("ALT")
}
if (event.metaKey) {
otherKeys.push("META")
}
let otherKeysStr = otherKeys.join(",")
if (otherKeysStr.length) {
otherKeysStr = `(${otherKeysStr})`
}
document.getElementById("debug").innerText += (` ${event.key} ${otherKeysStr}`)
event.stopImmediatePropagation()
event.stopPropagation()
event.preventDefault()
return false;
});
</script>
</body>
</html>
答案 0 :(得分:0)
根据lois6b的建议,您至少可以使用以下选项为用户提供不刷新页面的选项:
window.addEventListener('beforeunload', (event) => {
event.preventDefault();
event.returnValue = '';
return '';
})