简体   繁体   English

Cordova iOS:点击自定义自动建议无法识别导致键盘关闭

[英]Cordova iOS: Click on custom auto suggest not recognized cause keyboard closes

in my Cordova App I have a problem on iOS devices and I have no idea how to solve. 在我的Cordova应用程序中,我在iOS设备上遇到问题,我不知道如何解决。

I have a custom auto-suggest which shows up below an input field while typing. 我有一个自定义自动建议,在输入时显示在输入字段下方。 All is contained in a dialog box with "position: fixed;". 全部包含在“position:fixed;”对话框中。 Autocomplete is an unordered list. 自动填充是一个无序列表。 Click on < li > Element should place the selected text into the input. 单击<li>元素应将所选文本放入输入中。 The problem is, when user clicks on the li, the input loses focus, the keyboard disappears and the whole fixed dialog box "jumps" down and the click event is not recognized. 问题是,当用户点击li时,输入失去焦点,键盘消失,整个固定对话框“跳”下来,无法识别点击事件。 It is recognized when the keyboard already IS closed. 键盘已经关闭时会被识别。

I tried several workarounds, like giving focus back to input field immediately after blur. 我尝试了几种解决方法,比如在模糊后立即将焦点重新放回输入字段。 But it does not help. 但它没有帮助。 Keyboard closes and opens instead of just keeping opened. 键盘关闭并打开,而不是仅保持打开状态。

Any Ideas how to solve? 任何想法如何解决?

Here is a video showing the behaviour. 这是一个显示行为的视频。 It is recorded on the iOS Simulator but same behaviour on real iPhone 6s. 它记录在iOS模拟器上,但在真实iPhone 6s上的行为相同。

在此输入图像描述

I have found a solution now. 我现在找到了解决方案。 As I said the click event is not triggered when the keyboard hides, but a touchstart event is triggered. 正如我所说,键盘隐藏时不会触发click事件,但会触发touchstart事件。

So I did a workaround, looking for touchstart event followed by a blur event. 所以我做了一个解决方法,寻找touchstart事件,然后是blur事件。 If the touchstart-target does not receive a click event within a given time, I will trigger one. 如果touchstart-target在给定时间内没有收到点击事件,我将触发一个。 This works on my test iPhone 6s. 这适用于我的iPhone 6s测试。

Here is the code: 这是代码:

var iosTapTarget=null;
if (device.platform === 'iOS') {
  js.eventListener(document.body).add('iosTap', 'touchstart', function (e) {
    iosTapTarget = e.target;

    js.eventListener(iosTapTarget).add('iosTapClick', 'click', function(e) {
      // when the target receives a click, do not trigger another click
      if (iosTapTarget) js.eventListener(iosTapTarget).remove('iosTapClick', 'click');
      iosTapTarget = null;
    });

    // after short time unset the target
    window.setTimeout(function () {
      if (iosTapTarget) js.eventListener(iosTapTarget).remove('iosTapClick', 'click');
      iosTapTarget = null;
    }, 600)
  });

  // on each input fields listen for blur event and trigger click event on element received touchstart before
  var blurableElements = document.querySelectorAll('input[type=text],input[type=email],input[type=password],textarea');
  for (var j = 0; j < blurableElements.length; ++j) {
    js.eventListener(blurableElements[j]).remove('iosBlur', 'blur');
    js.eventListener(blurableElements[j]).add('iosBlur', 'blur', function () {
        window.setTimeout(function() {
          if (iosTapTarget) {
            js.eventListener(iosTapTarget).remove('iosTapClick', 'click');
            js.triggerEvent(iosTapTarget, 'click');
          }
        }, 50);
    });
  }
}

PS: Event handling comes from my own JS "framework" js.js available here: https://github.com/JanST123/js.js But you can use vanilla JS event handling or jQuery event handling too, of course. PS:事件处理来自我自己的JS“框架”js.js可以在这里找到: https//github.com/JanST123/js.js但是你当然也可以使用vanilla JS事件处理或jQuery事件处理。

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

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