<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>testing</title>
</head>
<body>
<input type="checkbox" id="test">
<script>
var t = document.getElementById("test");
t.onmousedown = function(e) {
if (e.button === 0)
t.checked = true;
else if (e.button === 2)
t.checked = false;
alert(e.button);
}
</script>
</body>
</html>
如果我离开行alert(e.button);
它在哪里,复选框点击行为与预期相同:都离开了,点击选中该复选框,所有右键单击取消选中该复选框。onmousedown事件事件对象的行为出现异常
如果我删除代码alert(e.button);
,那么突然间,左键单击将检查并立即取消选中该复选框,右键单击将不会执行任何操作,只会打开上下文菜单。
这是为什么发生?以及我能做些什么来使其行为与第一段中描述的相同,但没有alert(e.button);
?
'e.preventDefault()'? mousedown事件不会导致复选框发生更改,只会发生“click”事件,这是一个mousedown,后跟同一元素上的mouseup。 'alert()'会扰乱这种情况并阻止发生默认点击。 – nnnnnn
nope,这仍然导致问题不幸 – derp
您可以尝试取消'click'事件。但是(除了利益的缘故)你为什么要这样做?更改标准控件行为会让用户感到困惑,如果无法通过键盘或单键鼠标使用控件,则可访问性会失败。 – nnnnnn