【问题标题】:How do I focus an input when I click a button with JavaScript alone仅使用 JavaScript 单击按钮时如何聚焦输入
【发布时间】:2021-04-24 10:04:09
【问题描述】:

假设我有一个带有 id 的输入

<input type="text" id="inPut">

还有一个按钮

<button onclick="myFunc()"></button>

我如何编写一个函数(使用简单的 javascript),以便在单击按钮时输入焦点?

【问题讨论】:

    标签: javascript input focus buttonclick


    【解决方案1】:
    const myFunc = () => {
       const input = document.getElementById("inPut");
       input.focus();
    }
    

    【讨论】:

      【解决方案2】:

      这是一个非常基本的解决方案...

      但是你可以使用 focus 方法...

      https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/focus

      function myFunc() {
        document.getElementById("inPut").focus();
      }
      

      【讨论】:

        猜你喜欢
        • 2020-01-15
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        相关资源
        最近更新 更多