简体   繁体   English

Safari(移动版)IOS-在外部单击时输入不会失去焦点

[英]Safari (mobile) IOS - Input doesn't loses focus when clicking outside

I'm doing a search bar that gives results (similar to an autocomplete) but on iOS' Safari it doesn't loses focus when tapping outside of the input tag when the keyboard is open. 我正在做一个提供结果的搜索栏(类似于自动完成功能),但是在iOS的Safari上,当键盘打开时在输入标签之外点击时,它不会失去焦点。

I did a simple jsfiddle to demonstrate the minimal code that reproduces this issue: 我做了一个简单的jsfiddle来演示重现此问题的最少代码:

https://jsfiddle.net/coppolaemilio/0bqrLcwe/4/ https://jsfiddle.net/coppolaemilio/0bqrLcwe/4/

HTML HTML

<input class="selector" type="text">
<ul>
  <li>element</li>
</ul>

JS (jQuery) JS(jQuery)

$('.selector').focus(function() {
    $('ul').addClass('visible');
});
$('.selector').focusout(function() {
    $('ul').removeClass('visible');
});

CSS CSS

.selector{
  margin: 20px;
}
ul {
  display: none;
}
ul.visible {
  display: block;
}

I managed to find a solution by using this: 我设法使用以下方法找到解决方案:

$(document).on('touchstart', function (event) {
  if (!$(event.target).closest('.country-selector').length) {
    if ($('.country-selector').is(":visible")) {
      $('input.country-selector').blur();
    }
  }
});

https://jsfiddle.net/coppolaemilio/0bqrLcwe/5/ https://jsfiddle.net/coppolaemilio/0bqrLcwe/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM