2017-04-15 11 views
0
var cell_masuk = newRow.insertCell(5); 
    var newText = document.createTextNode(document.getElementById("tanggal_masuk").value); 
    cell_masuk.appendChild(newText); 

    var cell_exp = newRow.insertCell(6); 
    var newText = document.createTextNode(document.getElementById("tanggal_kadaluarsa").value); 
    cell_exp.appendChild(newText); 

    var cell_btn = newRow.insertCell(7); 
    var newText = document.createTextNode("<input type='button' class='button' value='x'>"); 
    cell_btn.appendChild(newText); 

從上面的代碼我想插入新的表格行上的按鈕。但是,當我使用此代碼:我如何添加一行按鈕與「document.createTextNode」

var cell_btn = newRow.insertCell(7); 
var newText = document.createTextNode("<input type='button' class='button' value='x'>"); 
cell_btn.appendChild(newText); 

我的表上的結果始終是字符串。 對不起,我的英語不好,即時通訊新的Web開發。

回答

2

顧名思義,createTextNode創建一個文本節點。您提供的內容是字面上的,作爲要顯示的字符。

如果你想創建一個input元素,你想createElement("input")

var cell_btn = newRow.insertCell(7); 
var input = document.createElement("input"); 
input.type = "button"; 
input.className = "button"; 
input.value = "x"; 
cell_btn.appendChild(input); 

另外,如果你喜歡用的標記工作,你可以使用insertAdjacentHTML

var cell_btn = newRow.insertCell(7); 
cell_btn.insertAdjacentHTML(
    "beforeend", 
    "<input type='button' class='button' value='x'>" 
); 
+0

你打我吧。 +1 –

+0

@ T.J。 Crowder,我嘗試用input.onClick = DeleteRow(index)在按鈕 上添加'onClick';但它不工作,你能告訴我正確的語法嗎?謝謝:) – Sebastian

相關問題