最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hiding in the iOS keyboard when user clicks outside of a text box - Stack Overflow

programmeradmin3浏览0评论

I'm attempting to hide the iPad soft keyboard when the user clicks outside a text field.

Specifically somewhere on the form or body that contains no other elements.

Using jquery, jquery mobile, and html5.

I'm attempting to hide the iPad soft keyboard when the user clicks outside a text field.

Specifically somewhere on the form or body that contains no other elements.

Using jquery, jquery mobile, and html5.

Share Improve this question asked Mar 8, 2012 at 15:32 yeowohyeowoh 1431 silver badge9 bronze badges 7
  • have you tried calling textfield.blur()? Of course you have to fetch the click before in any way. – calimarkus Commented Mar 8, 2012 at 15:50
  • keyboard should hide on the blur event of the input..if not the problem is elsewhere. show your code – gpasci Commented Mar 8, 2012 at 16:34
  • Right now using an Android tablet it functions perfectly. The user clicks outside the textbox, and the keyboard will collapse. I only have the code below, everything else I've tried has failed. var hideKeyboard = function() { document.activeElement.blur(); $("input").blur(); }; This is so the textbox will blur so the keyboard closes, if the user clicks any other element like radio buttons etc... Is there a way to have this work with the form or body as well? I'm a C programmer by trade, and javascript makes me want to rip my hair out. – yeowoh Commented Mar 8, 2012 at 17:20
  • do you have more then one textfield? – nycynik Commented Apr 17, 2012 at 22:29
  • setTimeout(function(){ $('Wrappingdiv').click(function(){console.log('!')}) },0) – James Andino Commented Nov 9, 2012 at 22:15
 |  Show 2 more comments

3 Answers 3

Reset to default 11
document.activeElement.blur();

using mattwindwer's solution (but with more details):

$(document).click(function() {
    document.activeElement.blur();
});

Calling focus() method on a button (even hidden buttons) hides the keyboard. I have used this in my ipad web app.

You can add a click listener to body and using target property of the event, you can determine when to hide the keyboard.

发布评论

评论列表(0)

  1. 暂无评论