<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>document对象</title>
<script type="text/javascript">
function checkAll(){
//根据指定name属性,查询返回多个标签对象集合
var hobbies=document.getElementByName("hobby");
//循环遍历hobbies对象中的元素
for( var i=0;i < hobbies.length;i++){
//设置 选择的状态为 选中
hobbies[i].checked=true;
}
}
function checkNo() {
var hobbies = document.getElementByName("hobby");
for (var i = 0; i < hobbies.length; i++) {
hobbies[i].checked = false;
}
}
function checkReverse(){
var hobbies=document.getElementByName("hobby");
for(var i=0;i <hobbies.length;i++){
if(hobbies[i].checked){
hobbies[i].checked=false;
}else{
hobbies[i].checked=true;
}
}
}
</script>
</head>
<body>
<input type="checkbox" name="hobby" value="java" /> java
<input type="checkbox" name="hobby" value="c++"/> c++
<input type="checkbox" name="hobby" value="c"/> c
<button onclick="checkAll();" >全选</button>
<button onclick="checkNo();" >全不选</button>
<button onclick="checkReverse();" >反选</button>
</body>
</html>
结果就是点击按钮毫无反应,不知道是为什么