我使用这个脚本,发现当用户单击文本链接时添加一个文本框,有什么办法可以让自动出现的每个新文本框将光标放在文本框中。
脚本如下:
function add(orderType) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("name", orderType + "[]");
var foo = document.getElementById(orderType);
//Append the element in page (in span).
//Create the option text
var a = document.createTextNode("Choice: ");
foo.appendChild(a);
foo.appendChild(element);
var br = document.createElement("br");
foo.appendChild(br);
i++;
}
非常感谢
最佳答案
将其附加到 foo
后调用 element.focus()
:
function add(orderType) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", "text");
element.setAttribute("name", orderType + "[]");
var foo = document.getElementById(orderType);
//Append the element in page (in span).
//Create the option text
var a = document.createTextNode("Choice: ");
foo.appendChild(a);
foo.appendChild(element);
var br = document.createElement("br");
foo.appendChild(br);
element.focus();//focus new input
i++;
}
关于javascript - 将焦点添加到添加的每个新文本输入上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19913451/