【发布时间】:2020-09-24 22:59:14
【问题描述】:
我正在尝试通过 javascript 动态添加单选按钮,因为单选按钮的数量是在运行时确定的。当我创建这样的单选按钮时,单选按钮会显示在标签顶部。但我希望标签显示在单选按钮旁边。下面是javascript代码。
x = matchInfoArray[0];
var values = [x.team1, x.team2];
var selecttag1=document.createElement("input");
selecttag1.setAttribute("type", "radio");
selecttag1.setAttribute("name", "match1");
selecttag1.setAttribute("value", values[0]);
selecttag1.setAttribute("id","match1");
var lbl1 = document.createElement("label");
lbl1.innerHTML = values[0];
div = document.getElementById("poll-content");
div.appendChild(selecttag1);
div.appendChild(lbl1);
selecttag1=document.createElement("input");
selecttag1.setAttribute("type", "radio");
selecttag1.setAttribute("name", "match1");
selecttag1.setAttribute("value", values[1]);
selecttag1.setAttribute("id","match1");
lbl1 = document.createElement("label");
lbl1.innerHTML = values[1];
div = document.getElementById("poll-content");
div.appendChild(selecttag1);
div.appendChild(lbl1);
'poll-content'是一个div,css如下。
.poll-content input[type="button"]
{
border: none;
margin-top: 100px;
outline: none;
height: 30px;
width: 250px;
background: #fb2525;
color: #fff;
font-size: 18px;
border-radius: 20px;
}
谁能帮我通过javascript解决这个问题。
【问题讨论】:
-
你能发一个minimal reproducible example吗? 元素 x 显示在元素 Y 的顶部而不是旁边,很多事情都可能导致这种情况。
display:block;、伪元素等等等等。一个可重现的例子会有很大帮助。 -
很难想象你在做什么。请做一个sn-p stackoverflow.com/help/minimal-reproducible-example
-
从我的脑海中,尝试:
lbl1.appendChild(selecttag1); div.appendChild(lbl1);。让我知道它是否有帮助/有效。 -
selecttag1.setAttribute("id","match1");上的错误(2 次)-> id 必须是唯一的! -
@iAmOren:非常感谢。它确实奏效了。你能把这个作为答案发布吗?
标签: javascript html