2

動的選択ボックスを作成しています。変更すると、テキスト セクションにデータが入力されます。追加するのではなく、データを何らかのテキストに設定したいと思います。

例えば:

この関数は、選択ボックスが変更されたときに呼び出されます。

function createDetails(...) {

    var details = document.getElementById("detailsDIV");
    var docFragment = document.createDocumentFragment();
    containerDiv = document.createElement("div");

    // Create five div sections with the text for the details section
    var nameTextNode    = createTextDivSect(nameStr);
    var phoneTextNode   = createTextDivSect("Phone:  " + phoneStr);
    var faxTextNode     = createTextDivSect("Fax:    " + faxStr);
    var websiteTextNode = createTextDivSect("Website: " + websiteStr);
    var emailTextNode   = createTextDivSect("Email: " + emailStr);

    // Append the text sections to the container DIV
    containerDiv.appendChild(nameTextNode);
    containerDiv.appendChild(phoneTextNode);
    containerDiv.appendChild(faxTextNode);

    // Add the container div to the doc fragment
    docFragment.appendChild(containerDiv);
    // Add the doc fragment to the div
    details.appendChild(docFragment);
}

////

このアプローチでは、セクションに追加していると想定しています。子を設定する方法はありますか。子を削除してから設定できると思います。しかし、それはリソースを浪費すると思います。

4

1 に答える 1

1

innerHTML を使用せずに div 全体を一度に設定する場合は、次を使用できますreplaceChild()

function createDetails(...) {

    var details = document.getElementById("detailsDIV");
    var docFragment = document.createDocumentFragment();

    // Process docFragment here...

    var parentNode = details.parentNode;
    parentNode.replaceChild(docFragment, details);
}
于 2009-06-02T14:36:25.497 に答える