强制软件键盘在HTML加载中显示焦点输入

时间:2018-03-11 13:24:41

标签: javascript html html5 smartphone soft-keyboard

使用简单的HTML页面,如何在智能手机上加载文件后以编程方式打开软件键盘? 为了更好的用户人体工程学,我们的目标是专注于已经打开软键盘的第一个输入。只是写下并继续。

没有用户的任何互动, 并在页面加载后, 既没有.focus()。click()和触发touchstart / touchend / mousedown / mouseup打开系统软键盘。

一个想法?...

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <script>
    var loadDOM = 0;
    var loadload = 0;

    function fullloaded() {
      if (loadDOM == 1 && loadload == 1) {
        var el = document.getElementById('test2');
        triggerEvent(el, 'touchstart');

        var el = document.getElementById('test2');
        triggerEvent(el, 'touchend');

        var el = document.getElementById('test2');
        triggerEvent(el, 'mousedown');

        var el = document.getElementById('test2');
        triggerEvent(el, 'focus');
        el.focus();

        var el = document.getElementById('test2');
        triggerEvent(el, 'mouseup');

        var el = document.getElementById('test2');
        triggerEvent(el, 'click');
        el.click();
      }
    }

    document.addEventListener('DOMContentLoaded', function(e) {
      logEvent('DOMContentLoaded');
      loadDOM = 1;
      fullloaded();
    });

    window.addEventListener("load", function(event) {
      var logEvent('load : All resources finished loading!');
      loadload = 1;
      fullloaded();
    });
  </script>
</head>

<body>
  <input id="test2" placeholder="_default_" />
</body>

</html>

1 个答案:

答案 0 :(得分:-1)

试试这个..

<input type="text" autofocus>

在javascript中试试这个

$(function() {
var el = document.getElementById('.class');
    el.focus();
});