如何使用 javascript 将光标放在 HTML <input type="text"> 标签上

How to use javascript to focus (get the cursor on it) on an HTML <input type="text"> tag

假设我有以下代码:-

    <div onclick="document.getElementById('inputfieldxyz').focus"> CLICK ME TO FOCUS ON THE INPUT TAG </div>

    <input id="inputfieldxyz" placeholder="This becomes ::focus 'ed when the div above is clicked">

所以...是的...我如何单击 div 并使 input 标签显示光标的方式与单击输入标签时的方式一样?

P.S.: 我不能使用jQuery。有没有办法使用普通 javascript 来做到这一点? (提前致谢:)

.focus() 不是 .focus

<div onclick="document.getElementById('inputfieldxyz').focus()"> CLICK ME TO FOCUS ON THE INPUT TAG </div>

    <input id="inputfieldxyz" placeholder="This becomes ::focus 'ed when the div above is clicked">

或者,HTML5 也支持 autofocus 属性:

<input type="text" autofocus />

与其使用带有附加 javascript 的 div,不如使用 <label for=id>,因为这正是它们的用途:

<label for='inputfieldxyz'> CLICK ME TO FOCUS ON THE INPUT TAG </label>
<input id="inputfieldxyz" placeholder="This becomes ::focus 'ed when the div above is clicked">