我有一个HTML表单,带有一些静态文本框和一个将创建动态文本框的按钮。现在,当按钮被点击时,我希望新创建的框被聚焦。我正在使用focus()方法,但它不起作用。另一件事是我会改变它在静态盒的工作背景颜色,但不是在动态框javascript动态文本框更改焦点
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";
}
请帮助的onfocus()方法。我是JS新手。由于
呸,那是什么'尝试catch'呢?我希望这不会留在你的代码中。 –
@JuanMendes绝对不会。我对JS很陌生,所以试图捕捉错误。 –
您的错误将显示在控制台中,不需要try catch –