由于在这个question中解释的问题,我有一种情况,我需要将mousewheel事件附加到下拉列表中,只有当它展开(我在onclick事件中这样做) 。不过,当列表崩溃时,我需要删除mousewheel事件。我如何去检测这个?Javascript检测下拉列表关闭时
我不能只使用onchange事件,因为用户可能没有真正改变他们的选择。我已经尝试了onblur事件,但在大多数浏览器(IE除外)下拉列表保持聚焦,当列表被折叠。
干杯。
var list = document.getElementById("list");
list.onclick = function (e) {
// attach mousewheel
list.onmousewheel = function (e) {
// ...
}
// attach click off
// This event fires fine in all browsers except FF when the list is expanded.
// In firefox it only fires when anywhere in the document is clicked twice.
// The first click closes the drop down list as expected and the second one
// fires the event.
window.document.onclick = function (e) {
list.onmousewheel = null;
window.document.onclick = null
}
};
编辑: 不幸的是,在Firefox MEDER的解决方案不起作用。文档上的点击事件不会被触发,直到我点击两次下拉列表。我该如何解决这个问题?它在IE中正常工作。
EDIT2: 我做了一些更多的测试与以下浏览器像预期的那样
- IE 7,
- 三价铬
- 歌剧院10
Firefox的要求在窗口中单击2次以使其工作& Safari根本不起作用。
看来,即使您点击下拉列表中的Firefox,仍然关注它。直到第二次点击发生,下拉列表才最终失去焦点。
你有链接或代码? – 2009-09-21 03:46:45