2014-03-04 102 views
0

我需要一些JavaScript幫助。我創建了一個函數divCreate我在這裏創建一個小表。然後我調用HTML頁面上顯示的那個函數。現在我有一些其他的值,所以我打電話給另一個功能,我想在該表中實施,並更改5和Rs 100 /升的價值,所以其他值不會發生.innerHTML。所有這些需要完成JavaScript,而不是jQuery。如何用JavsScript更改值


function divCreate(node){ 
    var div = document.createElement('div'); 
    var span = document.createElement('span'); 
    var img = document.createElement('img'); 
    var text = document.createTextNode('description'); 
    img.src = ""; 
    span.id = 'float-description'; 
    img.id = 'float-image'; 
    span.appendChild(text); 
    div.appendChild(span); 
    div.appendChild(img); 
    $table = "<table id = 'resultTable' padding=0px; textAlign =left;><tr><td width=30px>" + "Qty" + "</td>"; 
    $table += "<td>" + "Rate" + "</td></tr>"; 
    $table += "<td class='qty'>" + "5" + "</td>"; 
    $table += "<td class='rate'>" + "Rs 100/litre " + "</td></tr></table>"; 
    div.innerHTML += $table; 
    div.className = 'Desc'; 
    div.style.position = "absolute"; 
// div.style.textAlign = "center"; 
// div.style.visibility = "hidden"; 
    document.body.appendChild(div); 
} 

回答

1

您需要的nodeValue更新文本節點。

function changeValue(selector, value){ 
    var ele = document.getElementsByClassName(selector)[0].childNodes[0]; 
    ele.nodeValue = value; 
} 

http://jsfiddle.net/e7nXn/5/

+0

謝謝Sir..this是解決 – user3350333

+0

主席先生,我還有一個問題,我如何在不干擾表格部分的情況下將中心標籤置於中心位置。 – user3350333

+0

你是指一個td內的跨度? td span {text-align:center;} – JohanVdR

0

把5和Rs內有ID,然後改變它。

例如:

$table += "<td class='qty'><span id='number'> 5 </span></td>"; 
$table += "<td class='rate'><span id='rs'> Rs 100/litre </span></td></tr></table>"; 

使用再改:

var numberElement = document.getElementById('number'); 
numberElement.innerHTML = 'your value'; 
+0

不與.innerHTML – user3350333

+0

工作需要使用element.nodeValue – JohanVdR

+0

嘗試在控制檯,看看是否有你的頁面中的任何JS錯誤 – Oras