我试图用JavaScript创建一个单选按钮和一个标签,我让单选按钮工作了,但是标签没有出现在它旁边,我不知道我做错了什么。这是我的代码。
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);
你需要将标签追加到正文中,但你的代码将其追加为单选按钮的子项。
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);