<!DOCTYPE html>
<html>
<head>
<title>15th_test</title>
<meta charset="gb2312">
</head>
<body>
<h1>这是一个标题</h1>
<p>这是一段文字</p>
看书:<input type="checkbox" name="demo" value=1/><br>
写字:<input type="checkbox" name="demo" value=2/><br>
打飞机:<input type="checkbox" name="demo" value=3/><br>
玩游戏:<input type="checkbox" name="demo" value=4/><br>
<button type="button" οnclick="allCheck()">全选</button>
<button type="button" οnclick="allUnCheck()">取消全选</button>
<script>
function allCheck()
{
//不可以把name="demo"换成id="demo",name可以一样,id不可以一样
var dataList = document.getElementsByName("demo"); //注意获得数组是getElements,有s
for(var i=0; i<dataList.length; ++i)
{
dataList[i].checked = "checked";
}
}
function allUnCheck()
{
var dataList = document.getElementsByName("demo");
for(var i=0; i<dataList.length; ++i)
{
dataList[i].checked = null;
}
}
</script>
</body>
</html>
output: