【问题标题】:Problem with new inputs and focus attribute JS新输入和焦点属性 JS 的问题
【发布时间】:2020-04-17 18:58:53
【问题描述】:

我创建了一个简单的程序,如果我单击一个按钮,它会激活以下功能。我需要自动聚焦新的输入。我用focus()这个函数试过了,但是没用。

function moreInput() {
    let line = document.createElement("input");
    line.focus();
    line.classList.add("d-flex");
    line.classList.add("mt-3");
    line.classList.add("added-input");
    line.placeholder = "Nome studente";
    space.appendChild(line);
}

【问题讨论】:

  • 您需要在添加后将其聚焦。将该行移到末尾

标签: javascript function input focus


【解决方案1】:

在它被添加到 DOM 并且可见之前,您无法聚焦它。因此,在追加后将其移至末尾。

var space = document.body

function moreInput() {

  let line = document.createElement("input");
  line.classList.add("d-flex");
  line.classList.add("mt-3");
  line.classList.add("added-input");
  line.placeholder = "Nome studente";
  space.appendChild(line);
  line.focus();

}

moreInput()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多