我正在对网页设计进行一些 Psych 实验,我想禁用鼠标点击。 (请忽略可用性问题。我知道它们。我故意这样做是为了我的心理实验。)
到目前为止,我成功地禁用了 Firefox、Chrome 和 Safari 中的点击。然而,在 IE 中,当我左键单击时,焦点仍会更改到我单击的位置。我想停止这种行为(即焦点保持在我点击之前的位置)。系统是否产生警报并不重要,因为无论如何我都会使用警报。
如果你们中的一些人能帮助我,我将不胜感激。 请注意,我不能使用 JQuery。我的实验工具无法很好地处理 JQuery。
<html>
<head>
</head>
<body>
<div>
<select>
<option> aa </option>
</select>
</div>
<div>
<select>
<option> aa </option>
</select>
</div>
<script type="text/javascript">
function mousehandler(){
alert("For the purpose of psych experiment, you can't use mouse.");
return false;
}
document.oncontextmenu = mousehandler;
document.onmousedown = mousehandler;
</script>
</body>
</html>
最佳答案
不确定为什么 IE8 会这样做,但是如果您将第二个选择设置为在页面加载时获得焦点,那么在您的 mousehandler()
函数中添加 setTimeout(function( ){document.getElementsByTagName("select")[1].focus();},1);
在返回 false 之前你可以给第二个选择它的焦点。然后它似乎从未失去对第一个选择的关注。
setTimeout()
的原因是没有它第一个选择将获得焦点。
function mousehandler(){
alert("For the purpose of psych experiment, you can't use mouse.");
setTimeout(function(){document.getElementsByTagName("select")[1].focus();},1);
return false;
}
关于javascript - 禁用左键单击(用于心理实验),但在 IE 中焦点发生变化。如何预防?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5063135/