javascript - 将焦点添加到添加的每个新文本输入上

标签 javascript jquery

我使用这个脚本,发现当用户单击文本链接时添加一个文本框,有什么办法可以让自动出现的每个新文本框将光标放在文本框中。

脚本如下:

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/

相关文章:

javascript - 在提交表单之前使用 jquery 检查 $_POST 数据

javascript - 将网络图与 dc.js/crossfilter.js 集成

javascript - 指定javascript的src路径

javascript - 将触发多个悬停div的JS代码

.net - 从 Javascript 调用 Web 服务时,子域是否参与跨浏览器限制?

javascript - Jquery 函数查找具有 id 的第一个元素

javascript - 使用 Three.js 如何沿指定平面移动对象?

javascript - Angular 4/5 > httpclientmodule .get() 链接函数和返回数据类型

javascript - Scrollbottom js 在 Firefox、Chrome、Safari 中不起作用

javascript - 使用 jquery 进行分组选项选择