全选 不选 返选

Original 2019-03-27 17:48:04 177
abstract:<!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title> New Document </title>   </head> <body> &

<!DOCTYPE html>

<html>

 <head>

  <meta charset="UTF-8">

  <title> New Document </title>  

 </head>

 <body>

  

<!--<script>

var body = document.getElementById("body");

var childs = body.childNodes;

for (var i=0;i<childs.length;i++){

console.log(childs[i].nodeType);

 console.log(childs[i].nodeName);

  console.log(childs[i].nodeValue);

}

</script>-->


<div id="d1">

<input type="checkbox" name="class[]"/>语文

<input type="checkbox" name="class[]"/>数学

<input type="checkbox" name="class[]"/>英语

<input type="checkbox" name="class[]"/>政治


<br/>

<input type="checkbox" name="class[]"/>语文

<input type="checkbox" name="class[]"/>数学

<input type="checkbox" name="class[]"/>英语

<input type="checkbox" name="class[]"/>政治

<br/>

<input type="button" value="全选" id="all"/>

<input type="button" value="不选" id="none"/>

<input type="button" value="返选" id="prev"/>

</div>

<script>

var d1=document.getElementById("d1");

var childs=d1.childNodes;

var all=document.getElementById("all");

var none=document.getElementById("none");

var prev=document.getElementById("prev");



all.onclick=function(){

for(var i=0;i<childs.length;i++){


if(childs[i].nodeName=="INPUT"&&childs[i].type=="checkbox"){


childs[i].checked=true;

}


}


}

all.onclick=function(){

for(var i=0;i<childs.length;i++){


if(childs[i].nodeName == "INPUT"&&childs[i].type == "checkbox"){


childs[i].checked=false;


}


}


}

prev.onclick=function(){


for(var i=0; i<childs.length;i++){


id(childs[i].nodeName=="INPUT" && childs[i].type=="checkbox"){


if(childs[i].checked==true){childs[i].checked=false;


}else{

childs[i].checked=true;

}

}

}

}




</script>

 </body>

</html>


Correcting teacher:天蓬老师Correction time:2019-03-28 10:00:02
Teacher's summary:全选操作, 返回的是一组元素, 要用到一个数组来保存, 这点是关键

Release Notes

Popular Entries