一、排他思想
用for循环去除其他所有人的样式
二、获取,设置 属性值,移除属性
获取属性值
element.属性
获取内置属性值(元素本身自带属性)
element.getAttribute('属性')
获取自定义属性值
<div id="box"></div>
<script>
var div = document.querySelector('#box');
console.log(div.id);
console.log(div.getAttribute('id'));
</script>
设置属性值
element.属性 = '值';
设置内置属性值
element.setAttribute('属性, 值');
<div id="box"></div>
<script>
var div = document.querySelector('#box');
div.id = 'box2';
div.setAttribute('id', box2);
</script>
移除属性
element.removeAttribute('属性');
<div id="box"></div>
<script>
var div = document.querySelector('#box');
div.removeAttribute('id');
</script>
1. 按钮点击变色
<button>远近渔</button>
<button>远近渔</button>
<button>远近渔</button>
<button>远近渔</button>
<button>远近渔</button>
<script>
var btns = document.querySelectorAll('button');
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function () {
for (var i = 0; i < btns.length; i++) {
btns[i].style.backgroundColor = '';
}
this.style.backgroundColor = 'pink';
}
}
</script>
2. 百度换肤
<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>
body {
background: url(../imggg/傻笑.jpg) no-repeat fixed center center;
background-size: cover;
}
div {
width: 600px;
height: 150px;
margin: 100px auto;
border: 3px solid skyblue;
}
img {
float: left;
width: 150px;
height: 150px;
}
</style>
</head>
<body>
<div>
<img src="../imggg/傻笑.jpg" alt="">
<img src="../imggg/吃兔兔.jpg" alt="">
<img src="../imggg/沙雕.jpg" alt="">
<img src="../imggg/吐舌.jpg" alt="">
</div>
<script>
var img = document.querySelector('div').querySelectorAll('img');
for(var i = 0; i < img.length; i++) {
img[i].onclick = function() {
document.body.style.backgroundImage = 'url(' + this.src + ')';
}
}
</script>
</body>
3. 表格隔行变色
<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>
table {
border-collapse: collapse;
border: 3px solid skyblue;
}
</style>
</head>
<body>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>远近渔</th>
<th>鲸渔酱</th>
<th>渔渔子</th>
<th>哒咩哟</th>
</tr>
</thead>
<tbody>
<tr>
<td>远近渔</td>
<td>鲸渔酱</td>
<td>渔渔子</td>
<td><a href="#">哒咩哟</a></td>
</tr>
<tr>
<td>远近渔</td>
<td>鲸渔酱</td>
<td>渔渔子</td>
<td><a href="#">哒咩哟</a></td>
</tr>
<tr>
<td>远近渔</td>
<td>鲸渔酱</td>
<td>渔渔子</td>
<td><a href="#">哒咩哟</a></td>
</tr>
<tr>
<td>远近渔</td>
<td>鲸渔酱</td>
<td>渔渔子</td>
<td><a href="#">哒咩哟</a></td>
</tr>
<tr>
<td>远近渔</td>
<td>鲸渔酱</td>
<td>渔渔子</td>
<td><a href="#">哒咩哟</a></td>
</tr>
</tbody>
</table>
<script>
var trs = document.querySelector('tbody').querySelectorAll('tr');
for(var i = 0; i < trs.length; i++) {
trs[i].onmouseover = function() {
this.style.backgroundColor = 'pink';
}
trs[i].onmouseout = function() {
this.style.backgroundColor = '';
}
}
</script>
</body>
4. 全选反选
<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>
table {
border: 1px solid #ccc;
border-collapse: collapse;
}
thead tr {
background-color: skyblue;
}
tbody tr {
background-color: rgb(236, 235, 235);
}
</style>
</head>
<body>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>
<input type="checkbox" id="all">
</th>
<th>远近渔</th>
<th>鲸渔酱</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<input type="checkbox">
</th>
<td>远近渔</td>
<td>鲸渔酱</td>
</tr>
<tr>
<th>
<input type="checkbox">
</th>
<td>远近渔</td>
<td>鲸渔酱</td>
</tr>
<tr>
<th>
<input type="checkbox">
</th>
<td>远近渔</td>
<td>鲸渔酱</td>
</tr>
</tbody>
</table>
<script>
var all = document.querySelector('thead').querySelector('input');
var trs = document.querySelector('tbody').querySelectorAll('input');
// 上面选下面
all.onclick = function () {
for (var i = 0; i < trs.length; i++) {
trs[i].checked = this.checked;
}
}
// 下面选上面
for (var i = 0; i < trs.length; i++) {
trs[i].onclick = function() {
for(var i = 0; i < trs.length; i++) {
if (!trs[i].checked) {
all.checked = false;
break;
} else {
all.checked = true;
}
}
}
}
</script>
</body>
5. tab栏切换
<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>
* {
margin: 0;
padding: 0;
list-style: none;
box-sizing: border-box;
}
.tab {
width: 600px;
margin: 100px auto;
border: 3px solid skyblue;
}
.head {
width: 597px;
height: 40px;
}
.head ul {
height: 43px;
border-bottom: 3px solid skyblue;
}
.head ul li {
float: left;
width: 100px;
height: 40px;
text-align: center;
line-height: 40px;
}
.yuyu {
background-color: cornflowerblue;
color: #fff;
}
.body {
width: 100%;
height: 200px;
}
.body div {
display: none;
}
</style>
</head>
<body>
<div class="tab">
<div class="head">
<ul>
<li class="yuyu">远近渔</li>
<li>鲸渔酱</li>
<li>渔渔子</li>
<li>洗头渔</li>
<li>渔子酱</li>
</ul>
</div>
<div class="body">
<div class="box" style="display: block;">远近渔</div>
<div class="box">鲸渔酱</div>
<div class="box">渔渔子</div>
<div class="box">洗头渔</div>
<div class="box">渔子酱</div>
</div>
</div>
<script>
var lis = document.querySelector('.head').querySelectorAll('li');
var div = document.querySelector('body').querySelectorAll('.box');
for (var i = 0; i < lis.length; i++) {
lis[i].setAttribute('index', i);
lis[i].onclick = function () {
for (var i = 0; i < lis.length; i++) {
lis[i].className = '';
}
this.className = 'yuyu';
// 下面
for(var i = 0; i < lis.length; i++) {
div[i].style.display = 'none';
}
var a = this.getAttribute('index');
div[a].style.display = 'block';
}
}
</script>
</body>