简体   繁体   English

防止键盘关闭时出现模糊事件

[英]Prevent blur event on keyboard close

This is for mobile web apps, not native. 这是用于移动Web应用程序,而不是本机。

I have an autocomplete drop-down that closes on the blur event. 我有一个自动完成下拉菜单,在模糊事件中关闭。 But I want to prevent this from happening when the user closes the keyboard on mobile (ie the autocomplete dropdown should stay visible). 但是我想防止这种情况在用户关闭移动设备上的键盘时发生(即,自动完成下拉菜单应保持可见)。 Is there a way to distinguish a blur event caused by the keyboard closing, and other kinds of blur events? 有没有办法区分由键盘关闭和其他种类的模糊事件引起的模糊事件? Can I prevent a blur event specifically caused by closing the keyboard in mobile? 是否可以防止因关闭手机键盘而引起的模糊事件?

Ok, first I would recommend checking your libraries' documentation because they might provide something in their API, although there's nothing I'm aware of. 好的,首先,我建议您检查您的库的文档,因为它们可能会在其API中提供某些内容,尽管我什么都不知道。 That's the disadvantage of Web Apps: you can't access native functionality. 这是Web Apps的缺点:您无法访问本机功能。

If you still really want to do it, here's a possible solution. 如果您仍然真的想这样做,这是一个可能的解决方案。 It's ugly, but it might work. 这很丑陋,但可能会起作用。

Container on tap function (event)

If !autocomplete return // if you cant see the popup do nothing and blur normally

If (event.target != inputID) AND (event.target != autocID)
CloseAutocomplete()`

Essentially, instead of closing the autocomplete on blur, close it whenever the user taps on your parent container, but not on the input itself or the autocomplete. 本质上,不是在模糊时关闭自动完成功能,而是在用户点击父容器时将其关闭,而不是在输入本身或自动完成功能上将其关闭。 Depending on how it works, you could extend it to check any tap on screen. 根据其工作方式,您可以扩展它以检查屏幕上的任何水龙头。

I see you tagged iOS... you can observe keyboard events with UIKeyboardWillHideNotification. 我看到您标记了iOS ...您可以通过UIKeyboardWillHideNotification观察键盘事件。 [ https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/index.html#//apple_ref/c/data/UIKeyboardWillHideNotification] You may have to do and asynchronous delay to catch that event on with onblur, though. [ https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWindow_Class/index.html#//apple_ref/c/data/UIKeyboardWillHideNotification]您可能必须执行异步延迟才能捕获该事件但是使用onblur。

If you really want to prevent all "other kinds of blur events" you could make your drop-down close on specific events (instead of on blur). 如果您确实想防止所有“其他类型的模糊事件”,则可以使下拉列表针对特定事件(而不是模糊事件)关闭。 For example: when a another field is clicked, navigation, and/or add an 'x' close button next to that drop-down control. 例如:单击另一个字段时,导航和/或在该下拉控件旁边添加一个“ x”关闭按钮。

In the blur event listener, test for event.relatedTarget === null. 在模糊事件侦听器中,测试event.relatedTarget === null。 If focus has moved to a different element, event.relatedTarget will refer to that element, but if there is no focus (as is the case when the cause of the blur is keyboard close), event.relatedTarget will be null. 如果焦点已移到另一个元素,则event.relatedTarget将引用该元素,但是如果没有焦点(当模糊原因是键盘关闭时就是这种情况),event.relatedTarget将为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM