javascript 输入焦点时隐藏ios设备上的键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18384373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:42:53  来源:igfitidea点击:

hide keyboard on ios device when focus in input

javascriptjqueryhtmlcss

提问by alx lark

I want hide virtual keyboard on ipad device when some plugin ( or other code) set focus to input element on html page using pure javascript( or jquery library)

当某些插件(或其他代码)使用纯 javascript(或 jquery 库)将焦点设置为 html 页面上的输入元素时,我想在 ipad 设备上隐藏虚拟键盘

回答by R3tep

If you need a pure javascriptsolution, use this line :

如果您需要纯javascript解决方案,请使用以下行:

document.activeElement.blur();

This line removes the focus on the active elementand hiding the keyboard.

这一行移除了对活动元素的关注并隐藏了键盘。



Reference

参考

回答by ryan

You would have to blur it again but the keyboard might flash.

您将不得不再次模糊它,但键盘可能会闪烁。

$('input').on('focus', function() { $(this).blur(); });

Or, depending on if you have dynamically created inputelements.

或者,取决于您是否有动态创建的input元素。

$('#wrapper').on('focus', 'input', function() { $(this).blur(); });

回答by Dawid Paluchowski

Typescript version requires to fire blur()on HTMLElement. Like this:

打字稿版本需要blur()HTMLElement. 像这样:

let myElement: HTMLElement;
myElement = <HTMLElement>document.activeElement;
myElement.blur()

回答by egemen

Sometimes, it needs waiting for a while.

有时,它需要等待一段时间。

setTimeout(function() { document.activeElement.blur(); }, 100);