我有一个带有一些静态文本框和一个将创建动态文本框的按钮的 HTML 表单。现在,当单击按钮时,我希望新创建的框能够聚焦。我正在使用focus()
方法,但它不起作用。另一件事是我有一个onFocus()
方法可以改变在静态框中工作但在动态框中不工作的背景颜色。
function addPhone(){
try{
var phone = document.getElementById("phone");
phone.appendChild(document.createElement("Phone"+noOfPhones));
var textbox = document.createElement("input");
textbox.setAttribute("type", "textbox");
textbox.setAttribute("id","phone"+noOfPhones);
textbox.setAttribute("name","phone"+noOfPhones);
textbox.setAttributte('onFocus','onFocus(this);');
textbox.style.background="lightgrey";
document.getElementById("phone").appendChild(textbox);
phone.appendChild(document.createElement("br"));
textbox.focus();
noOfPhones++;
}catch(err){
alert(err);
}
}
function onFocus(element){
element.style.background = "lightgrey";
}
请帮忙。我是 JS 新手。