<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>全选/不选练习</title>
</head>
<body>
<form action="" method="post">
你爱好的运动是<input name="Checkbox" type="checkbox" id = "checkedAllbox"checked>全选/全不选
<br />
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" name="items" value="篮球" />篮球
<input type="checkbox" name="items" value="羽毛球" />羽毛球
<input type="checkbox" name="items" value="兵乓球" />乒乓球
<br />
<input type="button" id="checkedAllBtn" value="全选" / >
<input type="button" id="checkedNoBtn" value="全不选" / >
<input type="button" id="checkedRevBtn" value="反选" / >
<input type="button" id="sendBtn" value="提交" / >
</div>
</form>
<div id="index">
<div id="c">
<ul id="city">
<li id="bj">北京</li>
<li>上海</li>
<li>东京</li>
<li>首尔</li>
</ul>
</div>
<div id="btnlist">
<div><button type="button" id="btn01">创建一个广州节点</button></div>
<div><button type="button" id="btn02">将广州节点插入到bj前面</button></div>
<div><button type="button" id="btn03">使用广州节点替换bj节点</button></div>
<div><button type="button" id="btn04">删除广州节点</button></div>
<div><button type="button" id="btn05">读取city内的节点数量</button></div>
<div><button type="button" id="btn06">读取bj内的html代码</button></div>
</div>
</div>
<script type="text/javascript">
window.onload = function(){
var checkedAllBtn = document.getElementById("checkedAllBtn");
checkedAllBtn.onclick = function(){
var items = document.getElementsByName('items');
for(var i = 0;i<items.length;i++){
items[i].checked=true;
}
}
var checkedNoBtn = document.getElementById("checkedNoBtn");
checkedNoBtn.onclick = function(){
var items = document.getElementsByName('items');
for(var i = 0;i<items.length;i++){
items[i].checked=false;
}
}
var checkedRevBtn = document.getElementById("checkedRevBtn");
checkedRevBtn.onclick = function(){
var items = document.getElementsByName('items');
for(var i = 0;i<items.length;i++){
if(items[i].checked==false){
items[i].checked=true
}else{
items[i].checked=false
}
}
}
var sendBtn = document.getElementById("sendBtn");
sendBtn.onclick = function(){
var items = document.getElementsByName('items');
for(var i = 0;i<items.length;i++){
if(items[i].checked==true){
alert(items[i].value);
}
}
}
var checkedAllbox = document.getElementById("checkedAllbox");
checkedAllbox.onclick = function(){
var items = document.getElementsByName('items');
for(var i = 0;i<items.length;i++){
items[i].checked = checkedAllbox.checked;
}
}
var items = document.getElementsByName('items');
for(var i = 0;i<items.length;i++){
items[i].onclick = function(){
checkedAllbox.checked=true;
for(var j =0;j<items.length;j++){
if(!items[j].checked){
checkedAllbox.checked=false;
}
}
}
}
var body = document.getElementsByTagName('body')[0];
var body = document.body;
var html = document.documentElement;
var all = document.all;
var all = document.getElementsByTagName('div');
var box1 =document.getElementsByClassName('box1');
var div = document.querySelector('.box1 div');
myClick("btn01",function(){
var li = document.createElement("li");
var gztext = document.createTextNode('广州');
li.appendChild(gztext);
var city = document.getElementById('city');
city.appendChild(li);
});
myClick("btn02",function(){
var li = document.createElement("li");
var gztext = document.createTextNode('广州');
li.appendChild(gztext);
var bj = document.getElementById('bj');
var city = document.getElementById('city');
city.insertBefore(li,bj);
});
myClick("btn03",function(){
var li = document.createElement("li");
var gztext = document.createTextNode('广州');
li.appendChild(gztext);
var bj = document.getElementById('bj');
var city = document.getElementById('city');
city.replaceChild(li,bj);
});
myClick("btn04",function(){
var bj = document.getElementById("bj");
var city = document.getElementById('city');
bj.parentNode.removeChild(bj);
});
myClick("btn05",function(){
var city = document.getElementById('city');
alert(city.innerHTML);
});
myClick("btn06",function(){
var bj = document.getElementById('bj');
bj.innerHTML = "昌平";
});
var city = document.getElementById('city');
city.innerHTML +="<li>广州</li>"
function myClick(idStr,fun){
var btn = document.getElementById(idStr);
btn.onclick=fun;
}
}
</script>
</body>
</html>