Javascript 如何设置动态输入字段的焦点?

Javascript 如何设置动态输入字段的焦点?,javascript,html,Javascript,Html,我需要将焦点设置在输入字段上 按下按钮后,这种方法对我有效 function createNewDiv() { document.getElementById('myButton').blur(); ---- here I remove the focus from the button. Because the browser writes an error that an element with focus already exists cons

我需要将焦点设置在输入字段上

按下按钮后,这种方法对我有效

    function createNewDiv() {
      document.getElementById('myButton').blur(); ---- here I remove the focus from the button. Because the browser writes an error that an element with focus already exists
     
      const body = document.createElement('template');
      body.innerHTML = `
        <div>
          <input type="text" tabindex="2" onblur="document.getElementById('inputId').focus()" autofocus="autofocus" class="someClass"  id="inputId" placeholder="some text"  required>
        </div>
      `;

...
}
函数createNewDiv(){
document.getElementById('myButton').blur();——这里我从按钮中删除了焦点。因为浏览器写入了一个错误,即具有焦点的元素已经存在
const body=document.createElement('template');
body.innerHTML=`
`;
...
}
它创建一个包含输入字段的块。我想让这些信息集中起来。
在safari浏览器中,它工作并设置焦点。但是在google chrome中不起作用。

我将忽略您提供的代码,因为它是不完整的垃圾,缺少重要部分,引用无效,还有许多错误

所以我要告诉大家的是,在Chromium+Mozilla的世界里,为了关注某个元素,我们会这样做

e.focus(); e.select();

有关焦点的更多信息,请参见创建元素
const body=document.createElement('template')但你在哪里添加/插入它?鉴于你在回答中的评论,也许你是想投票“关闭”而不是回答,伙计们,我只是想在回去工作之前帮助2-3个人。