2010-09-24 44 views
1

我有javascript函數如下:試圖增加HTML節點,但它不會出現

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沒有出現,我也沒有收到任何錯誤。我究竟做錯了什麼?

+0

您測試過哪些瀏覽器?它可以在任何或沒有的情況下工作? – acrosman 2010-09-24 13:17:28

+0

它在IE8中運行的HTA中 – 2010-09-24 13:19:25

+0

剛剛在Firefox中以與.htm相同的結果嘗試過它 – 2010-09-24 13:23:04

回答

1

元素的nodeValue始終爲空。您想添加文本節點:

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

就是這樣。謝謝 – 2010-09-24 13:29:09

+0

我會盡快接受你的回答。 – 2010-09-24 13:29:46

相關問題