尝试添加HTML节点但不会出现

时间:2010-09-24 13:16:15

标签: javascript html dhtml

我有以下javascript函数:

    function addConfirmLine(number, strItem, strValue) {
        var confirmLine = document.getElementById("divConfirmation").appendChild(document.createElement("div"));
        confirmLine.id = "divConfirmLine" + number;

        var confirmItem = confirmLine.appendChild(document.createElement("div"));
        confirmItem.className = "confirmItem";
        confirmItem.nodeValue = strItem;

        var confirmValue = confirmLine.appendChild(document.createElement("div"));
        confirmValue.className = "confirmValue";
        confirmValue.nodeValue = strValue;
    }

div这样的

<div id="divConfirmation">
    <div class="checkHead">
        Check the following details.  Click "Prev" to make corrections.  Click "Upload" to process and upload the sermon.
    </div>
</div>

目的是最终得到类似的东西:

<div id="divConfirmation">
    <div class="checkHead">
        Check the following details.  Click "Prev" to make corrections.  Click "Upload" to process and upload the sermon.
    </div>
    <div id="divConfirmLine1">
        <div class="confirmItem">Item1</div>
        <div class="confirmValue">Value1</div>
    </div>
    <div id="divConfirmLine2">
        <div class="confirmItem">Item2</div>
        <div class="confirmValue">Value2</div>
    </div>
</div>

问题是它不起作用。新的div没有出现,我也没有收到任何错误。我做错了什么?

1 个答案:

答案 0 :(得分:1)

Element的nodeValue始终为null。您想要添加文本节点:

confirmItem.appendChild(document.createTextNode(strItem));
confirmValue.appendChild(document.createTextNode(strValue));