【问题标题】:How to fire android back button from JavaScript when the button is triangle facing down? (hide keyboard)当按钮是三角形朝下时,如何从JavaScript触发android后退按钮? (隐藏键盘)
【发布时间】:2019-02-22 12:51:27
【问题描述】:

我正在寻找一种方法来立即隐藏输入(键入文本)焦点上的键盘和保持焦点的输入(在铬中,使用 JavaScript)。尝试了一些事情,其中​​包括 history.back() 和 history.go(-1) 但这就是它所说的 - 页面返回。提前致谢。

【问题讨论】:

  • 隐藏键盘不取决于您(网站)。为什么要隐藏它?
  • @VladyslavMatviienko 我想将输入字段用作纯条形码扫描仪字段,而不是用虚拟键盘混淆用户

标签: javascript android jquery back-button


【解决方案1】:

这是一种适用于 Android 的方法。

function hideKeyboard() {
  //this set timeout needed for case when hideKeyborad
  //is called inside of 'onfocus' event handler
  setTimeout(function() {

    //creating temp field
    var field = document.createElement('input');
    field.setAttribute('type', 'text');
    //hiding temp field from peoples eyes
    //-webkit-user-modify is nessesary for Android 4.x
    field.setAttribute('style', 'position:absolute; top: 0px; opacity: 0; -webkit-user-modify: read-write-plaintext-only; left:0px;');
    document.body.appendChild(field);

    //adding onfocus event handler for out temp field
    field.onfocus = function(){
      //this timeout of 200ms is nessasary for Android 2.3.x
      setTimeout(function() {

        field.setAttribute('style', 'display:none;');
        setTimeout(function() {
          document.body.removeChild(field);
          document.body.focus();
        }, 14);

      }, 200);
    };
    //focusing it
    field.focus();

  }, 50);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多