由於在這個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