3

いくつかの静的テキスト ボックスと、動的テキスト ボックスを作成するボタンを含む HTML フォームがあります。ボタンをクリックすると、新しく作成されたボックスがフォーカスされます。focus()メソッドを使用していますが、機能していません。もう1つのことは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が初めてです。

4

1 に答える 1

1

試す:

<div id="phone"></div>
<input type="button" onclick="addPhone()" value="Add"/>
<script type="text/javascript">
var noOfPhones=0;
function addPhone(){
    try{
        noOfPhones++;
        var phone = document.getElementById("phone");
        phone.appendChild(document.createTextNode("Phone :"+noOfPhones));
        var textbox = document.createElement("input");
        textbox.setAttribute("type", "textbox");
        textbox.setAttribute("id","phone"+noOfPhones);
        textbox.setAttribute("name","phone"+noOfPhones);
        textbox.setAttribute('onfocus','onFocus(this, true);');
        textbox.setAttribute('onblur','onFocus(this, false);');
        textbox.style.background="white";
        document.getElementById("phone").appendChild(textbox);
        phone.appendChild(document.createElement("br"));
        textbox.focus();
    }catch(err){
        alert(err);
    }
}

function onFocus(element, hasFocus){
if(hasFocus)
    element.style.background = "lightgrey";
else
    element.style.background = "white";
}
</script>
于 2012-12-07T16:46:17.420 に答える