HTML Collection
// 不能对HTML Collection执行点击事件,只能对具体元素执行事件
/*
this -->在事件中,谁执行事件,this就指谁
*/
var all = document.getElementsByClassName("one");
for(var i=0;i<all.length;i++){
all[i].onclick = function(){
this.style.display = "none"
}
}
input checked onclick
<input id="input" type="checkbox" >篮球
<!--
点击事件 onclick
input输入框有checked,true表示选中,false没有选中
-->
<script>
var input = document.getElementById("input");
input.checked = false;
console.log(input.id)
</script>
<button id="all">全选</button>
<button id="noAll">不选</button>
<button id="reverse">反选</button>
<div>
<input type="checkbox">足球
<input type="checkbox">篮球
<input type="checkbox">Lol
<input type="checkbox">王者荣耀
</div>
<script>
var all = document.getElementById("all");
var noAll = document.getElementById("noAll");
var reverse = document.getElementById("reverse");
var inputs = document.getElementsByTagName("input");
/* 1.对btn执行点击事件 */
all.onclick = function () {
/* 2.将所有的input的checked属性设置为true */
for (var i = 0; i < inputs.length; i++) {
inputs[i].checked = true;
}
}
noAll.onclick = function () {
for (var i = 0; i < inputs.length; i++) {
inputs[i].checked = false;
}
}
reverse.onclick = function () {
for (var i = 0; i < inputs.length; i++) {
console.log(inputs[i].checked)
inputs[i].checked = (inputs[i].checked) ? false : true;
}
}
</script>
隔行变色
<style>
/*
odd 奇数
even 偶数
*/
/* li:nth-child(even){
background-color:pink;
}
li:nth-child(odd){
background-color: aqua;
} */
</style>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
/*
i%2 0,1,2,3,4
0,1,0,1,0
*/
var lis = document.getElementsByTagName("li");
for(var i=0;i<lis.length;i++){
if(i%2==0){
lis[i].style.backgroundColor = "pink"
}else{
lis[i].style.backgroundColor = "blue"
}
}
</script>
</body>
切换按钮
<div style="display: block;">
div
</div>
<button id="btn">btn</button>
<script>
var div = document.getElementsByTagName("div")[0];
var btn = document.getElementById("btn");
btn.onclick = function(){
var d = div.style.display;
// if(d=="block"){
// div.style.display = "none"
// }else{
// div.style.display = "block";
// }
div.style.display = (d=="block")?"none":"block";
}
</script>