当我实现这段代码时——复选框的名称不会在浏览器中与复选框一起显示——只是复选框本身。这是什么原因?我是否错误地使用了 setattribute-function?
<script type="text/javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("value", "car");
x.setAttribute("name", "vehicle");
document.body.appendChild(x);
</script>
最佳答案
您需要添加一个label
元素:
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("value", "car");
x.setAttribute("name", "vehicle");
document.body.appendChild(x);
var label = document.createElement("label");
label.textContent = "vehicle";
document.body.appendChild(label);
关于Javascript setattribute - 名称和值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243151/