how to close combobox when onmouseout?

后端 未结 3 1764
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 02:31

How to close combobox when onmouseout?


                        
    
提交评论

  • 2021-01-24 03:17

    A couple of years late but in case someone else is looking at this...I was able to toggle the open/close state of a select element in Chrome by sending it a mouse click. In this example moving the mouse over the toggle button will toggle the state of the <select>. This is a toggle, not an explicit open or close, but worked for me.

    <script type="text/javascript">
    
        function clickSelect(element) {
            var event = document.createEvent('MouseEvents');
            event.initMouseEvent('mousedown', true, true);
            element.dispatchEvent(event);
        };
    
        function toggleSelect() {
            clickSelect(document.getElementById("select"));
        }  
    </script>
    
    
    <form>
        <select id="select">
            <option>one</option>
            <option>two</option>
        </select>
        <input type="button" onmouseover="toggleSelect();" value="toggle select state" />
    </form>
    
    0 讨论(0)
  • 2021-01-24 03:25

    You can't do this (reliably). The popup that appears is not available to manipulate, ie open or close, programmatically and its behaviour is defined by the browser (or operating system).

    To expand on this further, both IE and Firefox can close the popup by blurring the select element:

    selectEl.blur();
    

    Although, the mouseout event fires even when you move the mouse to the options in the popup, so it would require a bit of hackery magic. In Chrome it will blur the select element but the box will remain open.

    It's generally best to leave the behaviour of UI components alone, so that users get the experience they expect through interaction with your website.

    0 讨论(0)
  • 提交回复
    热议问题