【问题标题】:Focus Input Box On Load加载时聚焦输入框
【发布时间】:2011-05-18 21:03:40
【问题描述】:

如何在页面加载时将光标聚焦在特定的输入框上?

是否也可以保留初始文本值并将光标置于输入末尾?

<input type="text"  size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />

【问题讨论】:

    标签: javascript html dom xhtml


    【解决方案1】:

    您的问题分为两部分。

    1) 如何将输入集中在页面加载上?

    您只需将autofocus 属性添加到输入。

    <input id="myinputbox" type="text" autofocus>
    

    但是,这可能不是所有浏览器都支持,所以我们可以使用 javascript。

    window.onload = function() {
      var input = document.getElementById("myinputbox").focus();
    }
    

    2) 如何将光标置于输入文本的末尾?

    这是一个非 jQuery 解决方案,其中有一些来自 another SO answer 的借用代码。

    function placeCursorAtEnd() {
      if (this.setSelectionRange) {
        // Double the length because Opera is inconsistent about 
        // whether a carriage return is one character or two.
        var len = this.value.length * 2;
        this.setSelectionRange(len, len);
      } else {
        // This might work for browsers without setSelectionRange support.
        this.value = this.value;
      }
    
      if (this.nodeName === "TEXTAREA") {
        // This will scroll a textarea to the bottom if needed
        this.scrollTop = 999999;
      }
    };
    
    window.onload = function() {
      var input = document.getElementById("myinputbox");
    
      if (obj.addEventListener) {
        obj.addEventListener("focus", placeCursorAtEnd, false);
      } else if (obj.attachEvent) {
        obj.attachEvent('onfocus', placeCursorAtEnd);
      }
    
      input.focus();
    }
    

    这是我如何使用 jQuery 完成此任务的示例。

    <input type="text" autofocus>
    
    <script>
    $(function() {
      $("[autofocus]").on("focus", function() {
        if (this.setSelectionRange) {
          var len = this.value.length * 2;
          this.setSelectionRange(len, len);
        } else {
          this.value = this.value;
        }
        this.scrollTop = 999999;
      }).focus();
    });
    </script>
    

    【讨论】:

    • 建议的第一个代码块实际上也将光标放在现有值的末尾,效果很好。感谢您的帮助。
    • Javascript 版本非常好,完美适用于 iframe 的自动对焦问题。也轻巧紧凑。 +1 因为它帮助了我。
    【解决方案2】:
    function focusOnMyInputBox(){                                 
        document.getElementById("myinputbox").focus();
    }
    
    <body onLoad="focusOnMyInputBox();">
    
    <input type="text"  size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text">
    

    【讨论】:

      【解决方案3】:

      一种可移植的方式是使用自定义函数(处理浏览器差异),例如this one

      然后在 &lt;body&gt; 标签的末尾为 onload 设置一个处理程序,正如 jessegavin 所写:

      window.onload = function() {
        document.getElementById("myinputbox").focus();
      }
      

      【讨论】:

        【解决方案4】:

        请注意 - 您现在可以在支持 HTML5 的浏览器中使用不带 JavaScript 的 HTML5:

        <input type="text" autofocus>
        

        您可能希望从这里开始,然后使用 JavaScript 构建它,以便为旧版浏览器提供后备。

        【讨论】:

        • 很高兴知道,这是否已经将光标移动到输入字段中的最后一个位置?
        • 我不认为@Codex73 试图完成 HTML5 中的 placeholder 属性所做的事情。听起来他希望光标自动定位在输入中当前值的末尾。
        • 你说得对,这不是一个完整的解决方案。但这确实解决了一些问题(加载自动对焦和占位符文本)。
        • 请注意,截至 1/2019,iOS 上的 Safari 不支持此功能:caniuse.com/#feat=autofocus
        【解决方案5】:
        $(document).ready(function() {
            $('#id').focus();
        });
        

        【讨论】:

        • 这需要 jQuery。
        • 是的,你需要包含 jQuery 库 :)
        • 经典的stackoverflow :)
        【解决方案6】:

        这对我来说很好用:

        <form name="f" action="/search">
            <input name="q" onfocus="fff=1" />
        </form>
        

        fff 将是一个全局变量,其名称绝对无关紧要,其目的是停止通用 onload 事件以强制关注该输入。

        <body onload="if(!this.fff)document.f.q.focus();">
            <!-- ... the rest of the page ... -->
        </body>
        

        发件人:http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html

        【讨论】:

          【解决方案7】:

          如果由于某种原因无法添加到 BODY 标记中,可以在表单之后添加:

          <SCRIPT type="text/javascript">
              document.yourFormName.yourFieldName.focus();
          </SCRIPT>
          

          【讨论】:

            【解决方案8】:

            工作正常...

            window.onload = function() {
              var input = document.getElementById("myinputbox").focus();
            }
            

            【讨论】:

              【解决方案9】:

              试试:

              纯Javascript:

              [elem][n].style.visibility='visible';
              [elem][n].focus();
              

              Jquery:

              [elem].filter(':visible').focus();
              

              【讨论】:

                【解决方案10】:

                非常简单的一行解决方案:

                <body onLoad="document.getElementById('myinputbox').focus();">
                

                【讨论】:

                  【解决方案11】:

                  将此添加到您的 js 的顶部

                  var input = $('#myinputbox');
                  
                  input.focus();
                  

                  或者到html

                  <script>
                      var input = $('#myinputbox');
                  
                      input.focus();
                  </script>
                  

                  【讨论】:

                    猜你喜欢
                    • 2020-12-25
                    • 1970-01-01
                    • 2022-01-15
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-03-08
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多