javascript 的一个小练习
实现复选框地全选;全不选;反选
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
//全选
function setAll() {
var num = document.getElementsByName("hobby");
for (var i = 0 ;i < num.length ;i++){
num[i].checked = true;
// checked 一种选中状态
}
}
//全不选
function buttfun() {
var num = document.getElementsByName("hobby");
for (var i = 0; i < num.length; i++) {
num[i].checked = false;
}
}
//反选
function buttfun2() {
var num = document.getElementsByName("hobby");
for (var i = 0; i < num.length; i++) {
num[i].checked=!num[i].checked;//取反
// if (loves[i].checked == false){
// loves[i].checked = true;
// } else{
// loves[i].checked = false;
// }
}
}
</script>
</head>
<body>
学习:
<input type="checkbox" name="hobby"value="app"checked="checked">Android
<input type="checkbox" name="hobby"value="html">html
<input type="checkbox" name="hobby"value="css">css
<input type="checkbox" name="hobby"value="js">js<br>
<button onclick="setAll()">全选</button>
<button onclick="buttfun()">全不选</button>
<button onclick="buttfun2()">反选</button>
</body>
</html>