Javascript detects dropdown is closed

Due to the problem described in this question , I came across a situation where I need to attach the mousewheel event to the drop-down list only when it expands (I do this in the onclick event). However, I need to remove the mousewheel event when the list crashes. How can I detect this?

I cannot just use the onchange event because the user may not have changed their choice. I tried the onblur event, but in most browsers (except IE) the jump list remains focused when the list is collapsed.

Greetings.

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
    }
};

EDIT: , meder firefox. , . ? IE.

EDIT2: ,

  • IE 7,
  • Chrome 3
  • Opera 10

Firefox 2 , , Safari .

, , firefox . , , .

+3
2

- ? , #el, , , , jQuery, DOM Scripting.

var dropdown = $('#el');
$(document).click(function(e){
    if ( (!$(e.target).is(dropdown)) || !$(e.target).closest('#el').length ) {
        // do what you need to
    }
});

, ?

PS - , , , .

+1

, , , <select> . , , , , select (IE), .

, <select> JavaScript <div> s. , . , , , , .

+1

Source: https://habr.com/ru/post/1718142/


All Articles