【问题标题】:keypress fail to work on mobile browser按键无法在移动浏览器上运行
【发布时间】:2017-02-20 15:14:15
【问题描述】:

此 Meteor 客户端事件在桌面浏览器上运行良好,但在移动浏览器“Chrome”上无法正常运行。

它检测“@”之后的“g”键条目并将其替换为“@gmail.com”。
知道如何让它在手机上工作吗?谢谢

Template.input.events({
  'keypress input': function (evt, template) {
    if (evt.which === 13) {
      //do stuff
    }
    else if (Session.get('taskSelected') === 'walk') {
      if (evt.which == 103) { // "g" has been typed do gmail.com
        utility.inputReplaceWith('gmail.com', evt);
      }
      else if (evt.which === 121) {  // "y" for yahoo.com
        utility.inputReplaceWith('yahoo.com', evt);
      }
      else if (evt.which === 104) {
        utility.inputReplaceWith('hotmail.com', evt);
      }
    }
  }
});

    inputReplaceWith: (text, evt) => {
      let elem = document.getElementsByName('email')[0].value;
      if (elem.slice(-1) == '@') { // last char is "@"
        evt.preventDefault();
        document.getElementsByName('email')[0].value = elem + text;
      }
    },

【问题讨论】:

标签: javascript meteor


【解决方案1】:

有一个textInput 事件可以为您提供输入的字符并且也可以取消

const inputField = document.getElementById('wanted-input-field');

inputField.addEventListener('textInput', function(e) {
    // e.data will be the 1:1 input you done
    const char = e.data; // In our example = "a"

    // If you want the keyCode..
    const keyCode = char.charCodeAt(0); // a = 97

    // Stop processing if "a" is pressed
    if (keyCode === 97) {
        e.preventDefault();
        return false;
    }
    return true;
});

【讨论】:

【解决方案2】:

Android 版 Chrome 中的 "keypress" 事件似乎很麻烦。

见:

TL;DR:改用"keydown" 和/或"keyup" 事件,甚至"input" 事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2020-07-03
    • 2017-02-18
    • 2012-04-11
    相关资源
    最近更新 更多