hide keyboard in iphone safari webapp

后端 未结 9 1887
长情又很酷
长情又很酷 2020-12-13 13:40

I\'m creating a webapp for the iPhone, based in HTML/CSS/JS. I\'m using forms to receive input and pass data to the script, but a problem I\'m encountering is that the keybo

相关标签:
9条回答
  • 2020-12-13 14:07

    You could try focus()ing on a non-text element, like the submit button.

    0 讨论(0)
  • 2020-12-13 14:10

    Here's a small code snippet that always hides the keyboard whenever the focus is in an input or textarea field and the user taps outside of that element (the normal behaviour in desktop browsers).

    function isTextInput(node) {
        return ['INPUT', 'TEXTAREA'].indexOf(node.nodeName) !== -1;
    }
    
    document.addEventListener('touchstart', function(e) {
        if (!isTextInput(e.target) && isTextInput(document.activeElement)) {
            document.activeElement.blur();
        }
    }, false);
    
    0 讨论(0)
  • 2020-12-13 14:16

    Be sure to set, in CSS:

    body {
      cursor: pointer;
    }
    

    otherwise, your event handler calling document.activeElement.blur() will never get fired. For more info, see: http://www.shdon.com/blog/2013/06/07/why-your-click-events-don-t-work-on-mobile-safari

    0 讨论(0)
提交回复
热议问题