我正在开发一个简单的游戏,用户点击“开始”按钮后,该按钮将被禁用,然后用户使用箭头键在画布上执行一些操作。在Chrome中,它的工作符合预期,但是我在Firefox中观察到了奇怪的行为。具体来说,在禁用按钮后,“keydown”事件不会在页面上触发,直到我在页面上单击某个位置。看起来像是在禁用按钮后,页面失去了焦点或其他情况。
这种行为是否符合规范,还是Firefox DOM事件分派中存在缺陷? Firefox版本为64.0,Ubuntu系统。
这种行为是否符合规范,还是Firefox DOM事件分派中存在缺陷? Firefox版本为64.0,Ubuntu系统。
const button = document.querySelector('button');
const canva = document.querySelector('.canva');
const doc = document.documentElement;
doc.addEventListener('keydown', evt => {
canva.innerHTML = evt.key;
});
button.addEventListener('click', evt => {
button.disabled = true;
});
body {
font-family: sans-serif;
margin: 0;
background-color: #F4511E;
}
button {
display: block;
text-align: center;
width: 100px;
margin: 10px auto;
}
.canva {
background-color: white;
width: 200px;
height: 200px;
margin: auto;
font-size: 1.5em;
font-weight: bald;
line-height: 200px;
text-align: center;
}
<!doctype html>
<html>
<head>
<title>Lost events</title>
<meta charset="UTF-8">
</head>
<body>
<button>Start</button>
<div class="canva"></div>
</body>
</html>
document.activeElement
时输出了什么? - Kaiidodocument.activeElement
是<body>
。在按钮点击和禁用之后:在 Chrome 上,它仍然是<body>
,但在 Firefox 上,它是禁用的按钮。因此,看起来禁用的按钮就像 DOM 事件的黑洞)document.activeElement.blur()
使<body>
再次活跃,并解决了问题。 - vatosarmat