js中的全选,不选,和反选按钮的设定

时间:2023-03-10 05:30:16
js中的全选,不选,和反选按钮的设定
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload=function()
{
var obtn1=document.getElementById("btn1");
var obtn2=document.getElementById("btn2");
var obtn3=document.getElementById("btn3");
var odiv=document.getElementById("div1");
var ach=odiv.getElementsByTagName("input");
obtn1.onclick=function()
{
for(i=;i<ach.length;i++)
{
ach[i].checked=true;
}
}
obtn2.onclick=function()
{
for(i=;i<ach.length;i++)
{
ach[i].checked=false;
}
}
obtn3.onclick=function()
{
for(i=;i<ach.length;i++)
{
if(ach[i].checked==true)//注意if里面的==等号不能和赋值号=混淆
{
ach[i].checked=false;
}
else
{
ach[i].checked=true;
}
}
}
}
</script>
</head> <body>
<input id="btn1" type="button" value="全选" /></br>
<input id="btn2" type="button" value="不选" /></br>
<input id="btn3" type="button" value="反选" /></br>
<div id="div1">
<input type="checkbox"/></br>
<input type="checkbox"/></br>
<input type="checkbox"/></br>
<input type="checkbox"/></br>
<input type="checkbox"/></br>
<input type="checkbox"/></br>
<input type="checkbox"/></br>
<input type="checkbox"/></br>
</div>
</body>
</html>