onclick() and onblur() ordering issue

前端 未结 6 1923
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 14:52

I have an input field that brings up a custom drop-down menu. I would like the following functionality:

  • When the user clicks anywhere outside the input field,
相关标签:
6条回答
  • 2020-12-04 15:23

    change onclick by onfocus

    even if the onblur and onclick do not get along very well, but obviously onfocus and yes onblur. since even after the menu is closed the onfocus is still valid for the element clicked inside.

    I did and it worked.

    0 讨论(0)
  • 2020-12-04 15:27

    You can use a setInterval function inside your onBlur handler, like this:

    <input id="input" onblur="removeMenu()" ... />
    
    function removeMenu() {
        setInterval(function(){
            if (!mouseflag) {
                document.getElementById('menu').innerHTML = '';
            }
        }, 0);
    }
    

    the setInterval function will remove your onBlur function out from the call stack, add because you set time to 0, this function will be called immediately after other event handler finished

    0 讨论(0)
  • 2020-12-04 15:32

    A more elegant (but likely less performant) solution:

    Instead of using the input's onblur to remove the menu, use document.onclick, which fires after onblur.

    However, this also means that the menu is removed when the input itself is clicked on, which is undesired behaviour. Set an input.onclick with event.stopPropagation() to avoid propagating clicks to the document click event.

    0 讨论(0)
  • 2020-12-04 15:40

    Replace on onmousedown with onfocus. So this event will be triggered when the focus is inside the textbox.

    Replace on onmouseup with onblur. The moment you take out your focus out of textbox, onblur will execute.

    I guess this is what you might need.

    UPDATE:

    when you execute your function onfocus-->remove the classes that you will apply in onblur and add the classes that you want to be executed onfocus

    and

    when you execute your function onblur-->remove the classes that you will apply in onfocus and add the classes that you want to be executed onblur

    I don't see any need of flag variables.

    UPDATE 2:

    You can use the events onmouseout and onmouseover

    onmouseover-Detects when the cursor is over it.

    onmouseout-Detects when the cursor leaves.

    0 讨论(0)
  • 2020-12-04 15:46

    I was having the exact same issue as you, my UI is designed exactly as you describe. I solved the problem by simply replacing the onClick for the menu items with an onMouseDown. I did nothing else; no onMouseUp, no flags. This resolved the problem by letting the browser automatically re-order based on the priority of these event handlers, without any additional work from me.

    Is there any reason why this wouldn't have also worked for you?

    0 讨论(0)
  • 2020-12-04 15:47

    onClick should not be replaced with onMouseDown.

    While this approach somewhat works, the two are fundamentally different events that have different expectations in the eyes of the user. Using onMouseDown instead of onClick will ruin the predictability of your software in this case. Thus, the two events are noninterchangeable.

    To illustrate: when accidentally clicking on a button, users expect to be able to hold down the mouse click, drag the cursor outside of the element, and release the mouse button, ultimately resulting in no action. onClick does this. onMouseDown doesn't allow the user to hold the mouse down, and instead will immediately trigger an action, without any recourse for the user. onClick is the standard by which we expect to trigger actions on a computer.

    In this situation, call event.preventDefault() on the onMouseDown event. onMouseDown will cause a blur event by default, and will not do so when preventDefault is called. Then, onClick will have a chance to be called. A blur event will still happen, only after onClick.

    After all, the onClick event is a combination of onMouseDown and onMouseUp, if and only if they both occur within the same element.

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