简体   繁体   English

如何在移动浏览器上监听输入事件?

[英]How do I listen for input events on mobile browsers?

I wrote a dumb little "web app" that consists of a simple text box that manipulates text and displays the output. Works as I expect on desktop, but not on mobile (Chrome and Safari on iOS).我写了一个愚蠢的小“网络应用程序”,它由一个简单的文本框组成,该文本框可以操作文本并显示 output。在桌面上按我预期的方式工作,但在移动设备上却不行(Chrome 和 Safari 在 iOS 上)。 I'm simply adding an event listener to an input, but the handler doesn't seem to be getting fired.我只是将事件侦听器添加到输入,但处理程序似乎没有被触发。 I've checked caniuse and it seems compatible, and haven't found any questions asked about this, so I'm hoping it's something I'm just overlooking in my code.我检查了 caniuse,它似乎兼容,但没有发现任何关于此的问题,所以我希望这是我在代码中忽略的东西。

Code snippet:代码片段:

const input = document.querySelector('#input'); // Yes, that's the input ID.
input.addEventListener('change', clapBack);

Link to full file:链接到完整文件:

https://github.com/martellaj/clap-back-generator/blob/master/js/main.js https://github.com/martellaj/clap-back-generator/blob/master/js/main.js

As @jam mok said, input isn't supported on mobile. 正如@jam mok所说,手机不支持输入。 Why not just use a change event? 为什么不只使用更改事件?

const input = document.querySelector('#input'); // Yes, that's the input ID.
input.addEventListener('change', clapBack);

For all humans still looking for an answer: onkeyup() does the trick. 对于仍在寻找答案的所有人类: onkeyup()可以解决问题。

So the solution for the mentioned code may look like: 因此,上述代码的解决方案可能如下所示:

const input = document.querySelector('#input'); // Yes, that's the input ID.
input.addEventListener('keyup', clapBack);

Of course it can also be used inline: <input name="inputText" onkeyup="clapBack()"> 当然也可以内联使用: <input name="inputText" onkeyup="clapBack()">

Updating for 2019: input and change is supported by the majority of popular mobile browsers: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event 在2019年进行更新:大多数流行的移动浏览器都支持inputchangehttps : //developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event

The following allows you to detect change on mobile browsers: 以下内容使您可以检测移动浏览器上的更改:

const input = document.querySelector('#input'); // Yes, that's the input ID.
input.addEventListener('change', clapBack);

Here's an example: https://codepen.io/anon/pen/LwVoWa 这是一个示例: https : //codepen.io/anon/pen/LwVoWa

Instead of using event .而不是使用事件 key or event .事件 code properties of keypress (keyup, keydown) event, I have used event . keypress (keyup, keydown) 事件的代码属性,我使用过event inputType , event .输入类型事件 data of update event and added few restricting attributes to the input tag to overcome 'smartness' of mobile keyboard app.更新事件的数据,并向输入标签添加了一些限制属性,以克服移动键盘应用程序的“智能”。 It is an ugly hack but worked for my purpose.这是一个丑陋的 hack,但对我的目的有用。

HTML: HTML:

<input type="text" id="inpt" autocapitalize="none" autocomplete="off" autocorrect="off" spellcheck="false" />

JS:记者:

var inpt = document.getElementById('inpt');
inpt.addEventListener('input', do_on_input);

function do_on_input(e) {
  
  if( e.inputType == "insertText"){
    console.log( e.data );
  }
  
  if( e.inputType == "deleteContentBackward"){
    console.log('Backspace');     
  }
  
  if( e.inputType == "insertCompositionText"){
    // without restrictive attribbutes you will need to deal with this type events
  }
  
}

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

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