1. 程式人生 > >4.1郵箱的全選,全不選,反選

4.1郵箱的全選,全不選,反選

example text -c style html function keyword if語句 基礎上

事件:onclick

屬性:checked

對於分清getElementsByTagName(‘元素名‘)裏的元素名,

可以先提取其外面一層的元素,再在此基礎上用getElementsByTagName(‘元素名‘)

用到for語句,if語句,length

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">

<link href="css1.css" rel="stylesheet" type="text/css"
charset="UTF-8">
</head>
<body>
<input id="btn1" type="button" value="全選"/>
<input id="btn2" type="button" value="不選"/>
<input id="btn3" type="button" value="反選"/>
<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>
<script src="js1.js"> </script>
</body>
</html>

///////////////////////js

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(var i=0;i<aCh.length;i++){
aCh[i].checked=true;
}

};
oBtn2.onclick=function(){
for(var i=0;i<aCh.length;i++){
aCh[i].checked=false;
}

};
oBtn3.onclick=function(){
for(var i=0;i<aCh.length;i++){
if(aCh[i].checked==true){
aCh[i].checked=false;
}
else{
aCh[i].checked=true;
}
}
};
};

4.1郵箱的全選,全不選,反選