
通过直接 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到父级:
采用 php+mysql 数据库方式运行的强大网上商店系统,执行效率高速度快,支持多语言,模板和代码分离,轻松创建属于自己的个性化用户界面 v3.5更新: 1).进一步静态化了活动商品. 2).提供了一些重要UFT-8转换文件 3).修复了除了网银在线支付其它支付显示错误的问题. 4).修改了LOGO广告管理,增加LOGO链接后主页LOGO路径错误的问题 5).修改了公告无法发布的问题,可能是打压
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号