我正在使用 javascript 来模拟禁用的选择元素。我实际上无法禁用它,因为 .net 验证器失败了,但那是另一回事了。
我有以下功能:
function preventFocus(e) {
if ($(this).hasClass("disabled")) {
e.preventDefault();
this.blur();
}
}
这里调用的是:
$("#ProvinceID").toggleClass("disabled").bind('focus click dblclick', preventFocus);
在IE中双击选择框似乎仍然可以显示和选择项目。我尝试了 IE 开发人员工具栏,e.type 显示为焦点和 dblclick。这是 IE 错误,还是我需要捕获其他事件?我也试过 focusin。
谢谢!
最佳答案
这晚了 4 年,但我刚刚发现实际上有一种方法可以让 IE ≤ 8 执行 e.preventDefault()
on mousedown
在其他浏览器(阻止选择 和 阻止焦点):设置 unselectable
属性! http://jsbin.com/yagekiji/1
请注意,与总是建议的解决方法不同,它总是归结为 setTimeout(function() { thingIDidntWantFocusStolenFrom.focus(); })
,这可以防止焦点被 窃取mousedown
目标放在首位!
unselectable
的有趣之处在于它不是继承的,因此它经常被忽视以支持 selectstart
事件(冒泡,和 e.preventDefault ()
阻止选择但不阻止焦点),或设置每个具有树遍历的后代元素(如 the StackOverflow answer that first clued me in that this might be possible ,或 Tim Down 的 series of nearly identical answers ),但实际上您可以在 mousedown
上将其设置在 event.target
上。
(另外,jQuery 票:http://bugs.jquery.com/ticket/10345)
关于javascript - IE - 如果选择框具有特定类,则阻止焦点(单击、双击),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2239378/