
通过直接 html 代码:
<div> <input type="checkbox" name="color" id="red"> <label for="red">red</label> </div> <div> <input type="checkbox" name="color" id="green"> <label for="green">green</label> </div> <div> <input type="checkbox" name="color" id="blue"> <label for="blue">blue</label> </div> <div> <input type="checkbox" name="color" id="yellow"> <label for="yellow">yellow</label> </div>
通过js代码,创建每个元素、属性、内容并将子级appendchild到父级:
iHuzuCMS狐族内容管理系统,是国内CMS市场的新秀、也是国内少有的采用微软的ASP.NET 2.0 + SQL2000/2005 技术框架开发的CMS,充分利用ASP.NET架构的优势,突破传统ASP类CMS的局限性,采用更稳定执行速度更高效的面向对象语言C#设计,全新的模板引擎机制, 全新的静态生成方案,这些功能和技术上的革新塑造了一个基础结构稳定功能创新和执行高效的CMS。iHuzu E
0
<body>
<div id="root"></div>
<script>
const root = document.getelementbyid("root");
const colors = ["red", "green", "blue", "yellow"];
colors.foreach((color) => {
// create id
const id = color;
// create label
const label = document.createelement("label");
label.setattribute("for", id);
// create checkbox input element
const input = document.createelement("input");
input.type = "checkbox";
input.name = "color";
input.id = id;
input.value = color;
// appendchild child to parent
label.appendchild(input);
label.appendchild(document.createtextnode(color));
root.appendchild(label);
});
</script>
</body>
通过js代码,带有innerhtml和模板文字:
<body>
<div id="root"></div>
<script>
const root = document.getElementById("root");
const colors = ["Red", "Green", "Blue", "Yellow"];
const checkbox = colors.map((color)=>`<label for="${color}">
<input type="checkbox" name="color" id="${color}" value="${color}" >
${color}</label>
`
).join("");
root.innerHTML = checkbox;
</script>
</body>
以上就是创建复选框的一些有效方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号