文章簡介:JS練習執行個體:javascript實現全選/全不選/反選. |
javascript實現全選/全不選/反選 功能
<!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=GBK" />
<style type="text/css">
*{
margin:0;
padding:0;
}
ul{
list-style-type:none;
}
#wrap{
width:150px;
margin:50px auto;
}
</style>
<title></title>
</head>
<body>
<div id="wrap">
<input type="button" id="selecte_all" value="全選"/>
<input type="button" id="selecte_no" value="全不選"/>
<input type="button" id="selecte_etr" value="反選"/>
<ul>
<li><input type="checkbox" class="about" />資料</li>
<li><input type="checkbox" class="about" />簡曆</li>
<li><input type="checkbox" class="about" />經曆</li>
<li><input type="checkbox" class="about" />愛好</li>
<li><input type="checkbox" class="about" />其他</li>
</ul>
</div>
<script type="text/javascript">
window.onload=init;
function init(){
var selecte_all=document.getElementById('selecte_all');
var selecte_no=document.getElementById('selecte_no');
var selecte_etr=document.getElementById('selecte_etr');
var check_list=document.getElementsByClassName('about');
selecte_all.onclick=function(){
for(i=0;i<check_list.length;i++){
check_list[i].checked=true;
};
}
selecte_no.onclick=function(){
for(i=0;i<check_list.length;i++){
check_list[i].checked=false;
};
}
selecte_etr.onclick=function(){
for(i=0;i<check_list.length;i++){
check_list[i].checked=!check_list[i].checked;
};
}
}
</script>
</body>
</html>