can't blur input in ios

2019-08-21 12:14发布

问题:

I'm building an iPad app using Xcode, Cordova and HTML files.

In testing the HTML on an iPad, I'm not able to blur my input fields by clicking outside them.

Do I need to code the background to blur the input?

I want the blur so I can hide the keyboard.

Unless there's a better solution to hiding it that does not need a blur().

回答1:

I found two code snippets that combine to trigger blur on inputs when clicking anywhere outside the input, or when pressing the Return key.

function isTextInput(node) {
    return ['INPUT'].indexOf(node.nodeName) !== -1;
}
document.addEventListener('touchstart', function(e) {
    if (!isTextInput(e.target) && isTextInput(document.activeElement)) {
    document.activeElement.blur();
    }
}, false);
$('input').keyup(function(event) {
    if (event.which === 13) {
      $(this).blur();
    }
});