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);
从上面的代码中,我想插入带有按钮的新表行。但是当我使用这段代码时:
var cell_btn = newRow.insertCell(7);
var newText = document.createTextNode("<input type='button' class='button' value='x'>");
cell_btn.appendChild(newText);
我表上的结果始终是字符串。 抱歉我的英语不好,我是网络开发新手。
最佳答案
顾名思义,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'>"
);
关于javascript - 如何添加带有 "document.createTextNode"按钮的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43427611/