If you put Input.focus () in a settimeout, you can get the focus.
Copy Code code as follows:
<script type= "Text/javascript" >
(function () {
function get (ID) {
return document.getElementById (ID);
}
Window.onload = function () {
Get (' Makeinput '). onmousedown = function () {
var input = document.createelement (' input ');
Input.setattribute (' type ', ' text ');
Input.setattribute (' value ', ' test1 ');
Get (' Inpwrapper '). appendchild (input);
Input.focus ();
Input.select ();
}
Get (' Makeinput2 '). onmousedown = function () {
var input = document.createelement (' input ');
Input.setattribute (' type ', ' text ');
Input.setattribute (' value ', ' test1 ');
Get (' Inpwrapper2 '). appendchild (input);
settimeout (function () {
Input.focus ();
Input.select ();
}, 0);
}
Get (' input '). onkeypress = function () {
Get (' preview '). InnerHTML = This.value;
}
}
})();
</script>
<button id= "Makeinput" > Generation input</button>
<p id= "Inpwrapper" ></p>
<H2>2, use <code>setTimeout</code><button id= "Makeinput2" > Generation input</button><p id= "Inpwrapper2" ></p>
<p><input type= "text" id= "input" value= ""/><span id= "Preview" ></span></p>