hide keyboard in iphone safari webapp

后端 未结 9 1889
长情又很酷
长情又很酷 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: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);
    

提交回复
热议问题