新输入和焦点属性 JS 的问题

Problem with new inputs and focus attribute JS

我创建了一个简单的程序,如果我点击一个按钮,它会激活以下功能。我需要自动对焦新输入。我用函数 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);
}

在将其添加到 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()