反选
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input class="like" type="checkbox">足球
<input class="like" type="checkbox">篮球
<input class="like" type="checkbox">排球
<input class="like" type="checkbox">游泳 <br>
<button id="reverse">反选</button>
<script>
/*
input输入框有属性checked
true 选中
false 没有选中
*/
var inputs = document.getElementsByClassName("like");
var reverse = document.getElementById("reverse");
reverse.onclick = function(){
for(var i=0;i<inputs.length;i++){
console.log(inputs[i].checked)
// if(inputs[i].checked == true){
// inputs[i].checked = false;
// }else{
// inputs[i].checked = true;
// }
inputs[i].checked = (inputs[i].checked == true)? false:true;
}
}
</script>
</body>
</html>
奇偶数变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/*
1
odd 奇数
even 偶数
*/
/* li:nth-child(odd){
color:red;
}
li:nth-child(even){
color:orange;
} */
</style>
</head>
<body>
<ul>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<button id="one">所有偶数的颜色变red</button>
<button id="two">所有奇数的颜色变orange</button>
<script>
/* 0 */
var lis = document.getElementsByTagName("li");
var one = document.getElementById("one");
var two = document.getElementById("two");
one.onclick = function(){
for(var i=0;i<lis.length;i++){
if(i%2==0){
lis[i].style.color="red"
}
}
}
two.onclick = function(){
for(var i=0;i<lis.length;i++){
if(i%2!=0){
lis[i].style.color="orange"
}
}
}
</script>
</ul>
</body>
</html>