我有一個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 –