[英]JavaScript creating a radio button and a label
我正在尝试创建一个单选按钮和一个带有 JavaScript 的 label 并且我让单选按钮工作,但是 label 并没有出现在它旁边,我无法弄清楚。 这是我的代码:
const x = document.createElement("INPUT");
x.setAttribute("type", "radio");
document.body.appendChild(x);
const y = document.createElement("LABEL");
const t = document.createTextNode("Label text");
y.textContent = "Label text";
y.setAttribute("for", "lord");
y.appendChild(t);
您需要 append 和 label 到正文,但您的代码将其作为单选按钮的子项附加
const x = document.createElement("INPUT"); x.setAttribute("type", "radio"); x.setAttribute("id", "lord"); document.body.appendChild(x); const y = document.createElement("LABEL"); const t = document.createTextNode("Label text"); y.textContent = "Label text"; y.setAttribute("for", "lord"); document.body.appendChild(t);
您必须附加到文档,例如单选按钮
document.body.appendChild(y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.