如何检测鼠标松开时触发的模糊?
我正在监听表单输入上的模糊事件。现在,当在控件之外按下鼠标时,该事件立即被触发。我需要能够检测鼠标何时在输入之外完全单击(鼠标按下然后鼠标向上)。
是否已经有一个我可以监听的事件类型可以处理这个问题?如果没有,处理此类事件的最佳方法是什么?
I am listening for a blur event on a form input. Right now, the event immediately gets triggered when the mouse is pressed down outside of the control. I need to be able to detect when the mouse is completely clicked outside of the input (mouse down and then mouse up).
Is there already an event type that I can listen to that will handle this? If not, what is the best way to handle this type of event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想说不,没有任何此类事件。我不认为有一个完美的方法来处理,
我会尝试通过
当表单获得焦点时向文档添加一个
mousedown
处理程序。它将把全局变量设置为 1。event.target
)。如果是这种情况,则设为 1,否则设为 0。mousedown
处理程序将具有event.preventDefault;
并return false;
。 这可能会给您的其他事件处理程序带来一些麻烦。为了避免这样的麻烦,我将尝试捕获在捕获阶段可能被损坏的事件,而不是冒泡事件。mouseup
处理程序。在其中,测试变量的值是否为 0。如果是,则执行模糊工作并删除两个特殊处理程序。但这可能有一些弱点(特别是如果用户在按下鼠标时离开窗口)。
我希望这是清楚的,我会尝试尽快发布小提琴。
编辑:这是小提琴。但请注意,我强制将焦点放在表单上,但它只起作用。我这样做是因为看起来表单永远不会获得焦点(可能与 jsfiddle 处理事件的方式有关)。但从理论上讲,这应该在没有
$("form").focus();
行的情况下工作。作为旁注,我使用 jQuery 进行一些简写,如果需要,我将尝试删除这些调用。
I'd say no, there aren't any event of that type. And I don't think there is a perfect way to handle that
I'll try to handle it this way
adding a
mousedown
handler to the document when the form get the focus. It will set a global variable to 1.event.target
). If it's the case, let 1, else set it to 0.The
mousedown
handler will haveevent.preventDefault;
andreturn false;
. This may cause some trouble to your others eventHandlers. To avoid such trouble, I'll try to capture the events that could be damaged on the capturing phase, not the bubbling one.mouseup
handler to the document when the form get the focus. In it, test if the variable has a value of 0. if yes, then do the blur job and remove the two special handlers.But this might have some weakness (especially if the user leaves the window while the mouse is pressed).
I hope this is clear, I'll try to post a fiddle asap.
EDIT: Here is the fiddle. However note that I force the focus on the form and it works only one.I did so because it looks like the form never get focused otherwise (probably linked to the way jsfiddle handle events). But in theory that should work without the
$("form").focus();
line.As a side note, i used jQuery for some shortands I'll try to remove the calls if needed.
嗯,我想在思考了一些之后我得到了一些东西。
我对我的语法仍然有点动摇,但我认为这可能会满足您的需求。
这个想法是,你捕获 .blur 中的控件 id,然后在 mouseup 中检查控件 id 与 .blur 控件 id,如果它们不相等,你就会失去焦点,并且可以在 mouseup 中执行你想要的操作,另外,在触发 mouseup 事件后,不要忘记清除模糊控件 id,否则它将触发多次鼠标 up 事件。
hmmm i think i got something after thinking about this for a few.
I'm still a little shakey with my syntax, but I think this may do what your looking for.
The idea is that you capture the control id in the .blur, and then in your mouseup you check that controls id against your .blur control id and if they are not equal you have lost focus and can do what you want in the mouseup, also dont forget to clear out your blur control id after you fire your mouseup event or it will fire for multiple mouse ups.
您可以在 body 标记的 onmouseup 属性中调用函数。
在该函数中,您当然必须引用有问题的对象,但如果没有您的代码示例,我无法提供进一步的帮助。
编辑:在不知道目的的情况下,我可以给出特定类型目的的粗略示例...
Javascript:
这是我在类似情况下无数次使用的方法,并且从未以任何方式出现问题。
You can call a function in the onmouseup attribute of the body tag.
In that function you will have to reference the object in question of course, but without example of your code I cannot offer further help.
Edit: Without knowing the purpose I can give a rough example of a particular type of purpose...
Javascript:
This is the approach I have used countless times for similar circumstances and has never been problematic in any way.
我遇到了类似的问题,并以这种方式处理它(我使用 jQuery 来处理事件,但你当然可以使用 addEventListener 代替):
I ran into a similar problem, and handled it this way (I used jQuery for events, but you can of course use addEventListener instead):