[英]How to add a line break after each dynamically created checkbox?
Here's the code for the dynamically created checkboxes. 这是动态创建的复选框的代码。 How do I add line breaks to the code so that I have one checkbox per line? 如何在代码中添加换行符,以便每行有一个复选框? I tried to do document.createElement("br")
and appending it after get.appendChild(label)
but it didn't work. 我试图做document.createElement("br")
并在get.appendChild(label)
之后附加它但是它不起作用。
function setCheckboxes(browser) {
var get = document.getElementById("get");
if (browser == "courses") {
for (var i = 1; i < coursesGetKeys.length; i++) {
var checkBox = document.createElement("input");
var label = document.createElement("label");
checkBox.type = "checkbox";
checkBox.value = coursesGetKeys[i];
checkBox.name = "r";
label.textContent = setOpt(coursesGetKeys[i], browser);
get.appendChild(checkBox);
get.appendChild(label);
//label.appendChild(document.createTextNode(setOpt(validCoursesKeys[i], dataset)));
}
}
if (browser == "rooms") {
for (var i = 1; i < validRoomsKeys.length; i++) {
var checkBox = document.createElement("input");
var label = document.createElement("label");
checkBox.type = "checkbox";
checkBox.value = validRoomsKeys[i];
checkBox.name = "r";
label.textContent = setOpt(validRoomsKeys[i], browser);
get.appendChild(checkBox);
get.appendChild(label);
//label.appendChild(document.createTextNode(setOpt(validCoursesKeys[i], dataset)));
}
}
};
You already know how to create an input: 您已经知道如何创建输入:
var checkBox = document.createElement("input");
and how to append it: 以及如何追加它:
get.appendChild(checkBox); // get is the parent element you selected at the top of your code
So your first hunch was correct, you can also do this: 所以你的第一次预感是正确的,你也可以这样做:
var br = document.createElement("br");
get.appendChild(br);
which creates a br element, and then also appends it to the "get" parent. 它创建一个br元素,然后将它附加到“get”父元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.