我有这样的JS代码:
var option = document.createElement("input");
option.id = "id";
option.type = "radio";
var txt = document.createTextNode("sample");
option.appendChild(txt);
但是这个“样本”在网站上是不可见的。 HTML 结构如下(在 Firebug 中):
<form id="answers"><input type="radio" id="id">sample</input></form>
但默认的 Firefox 检查器是:
<form id="answers"><input type="radio" id="id"></input></form>
我尝试过使用innerHTML,但存在同样的问题。我只是没有看到 <input>
之间的任何内容标签。
您有什么建议?
最佳答案
您创建元素但不将它们附加到表单中。你应该使用这样的东西:
document.getElementById('answers').appendChild(option);
通过这种方式,您将把新创建的选项元素附加到 ID 为“answers”的表单中。
请记住,在输入类型的单选按钮中包含文本在概念上是错误的!
关于javascript - JS/DOM 中不可见的 TextNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25384868/