2014-03-04 100 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