onclick() and onblur() ordering issue

泄露秘密 提交于 2019-11-28 05:09:57

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?

HIRA THAKUR

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.

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.

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.

user5271069

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!