JavaScript?排他思想的具體實現(xiàn)
在前面的博客中,小熊更新了相關(guān)操作元素的方法,但是如果有同一組元素,我們想要某一個元素實現(xiàn)某種樣式,這時需要怎么辦呢? 這里就要用到循環(huán)的排他思想。
排他思想的算法就是:
排除掉其他的(包括自己),然后再給自己設(shè)置想要實現(xiàn)的效果??偠灾?,排他思想的實現(xiàn)步驟就是所有元素全部清除與設(shè)置當(dāng)前元素。
可以簡單理解為:
- 所有元素全部清除樣式(干掉其他人)
- 給當(dāng)前元素設(shè)置樣式 (留下我自己)
需要注意的是:這里的順序不能顛倒。
比如,頁面有五個按鈕,我們想要給它實現(xiàn)循環(huán)點擊事件:當(dāng)點到哪個按鈕,就讓哪個按鈕變色,應(yīng)該怎樣操作呢?
1、我們先創(chuàng)建五個按鈕。
如下所示:
<button>按鈕1</button>
<button>按鈕2</button>
<button>按鈕3</button>
<button>按鈕4</button>
<button>按鈕5</button>
2、獲取元素
<script>
//獲取元素
var btn = document.getElementsByTagName('button');
console.log(btn);
</script>
3、循環(huán)遍歷打印按鈕
for(var i =0; i<btn.length;i++){
console.log(btn[i]
}
4、在第一個for循環(huán)里面給每個按鈕添加點擊事件。首先在內(nèi)循環(huán)里面清除掉所有按鈕的樣式,然后在外循環(huán)里給當(dāng)前點擊的按鈕添加樣式。
btn[i].onclick = function(){
for(var j =0;j<btn.length;j++){
btn[j].style.backgroundColor = '';
}
this.style.backgroundColor = 'blue';
}
最終效果為:

接下來我們舉幾個例子看看吧!
1、實現(xiàn)簡單的tab欄切換的功能
代碼如下:
<!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>
<!-- 編寫一個完整的tab切換效果的頁面 -->
<style>
* {
margin: 0;
padding: 0;
}
.box_1 {
width: 800px;
height: 400px;
background-color:rgb(141, 169, 173);
margin: 100px auto;
}
ul {
position:absolute;
top: 64px;
left:220px;
height: 35px;
line-height: 35px;
text-align: center;
}
li {
width: 80px;
height: 35px;
list-style: none;
float: left;
border: 1px solid #ccc;
margin-left: 2px;
border-top-left-radius: 6px;
border-top-right-radius: 6px ;
}
.li1 {
font-weight: 700;
color: black;
border-bottom: none;
background-color: skyblue;
cursor: pointer;
}
.item{
display:none;
}
</style>
</head>
<body>
<div class = 'box'>
<ul>
<li class='li1'>標(biāo)簽一</li>
<li>標(biāo)簽二</li>
<li class = 'li2' style="width:150px">自適應(yīng)寬度的標(biāo)簽</li>
</ul>
<div class="box_1">
<div class="item" style = "display:block">第一個標(biāo)簽的內(nèi)容</div>
<div class="item">第二個標(biāo)簽的內(nèi)容</div>
<div class="item">自適應(yīng)寬度的標(biāo)簽的內(nèi)容</div>
</div>
</div>
<script>
var li = document.querySelectorAll('li');
console.log(li);
var item = document.querySelectorAll('.item');
console.log(item);
for(var i =0;i<li.length;i++){
li[i].setAttribute('index',i);
li[i].onclick = function(){
for(var j =0;j<item.length;j++){
li[j].className = '';
console.log(li[i]);
}
this.className = 'li1';
var index = this.getAttribute('index');
console.log(index);
for(var k = 0;k<item.length;k++){
item[k].style.display='none';
}
item[index].style.display = 'block';
}
}
</script>
</body>
</html>
實現(xiàn)效果為:

2、實現(xiàn)一個動態(tài)點擊的調(diào)查結(jié)果顯示頁面,要求當(dāng)點擊復(fù)選框選項時對應(yīng)的進度條增加。
<!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>
.box{
width: 700px;
margin: 10px auto;
}
.bar {
width:200px;
height: 15px;
padding: 1px;
background-color: rgb(250, 249, 249);
}
.bar_in{
width:7%;
height:100%;
transition: width 0.5s;
}
.bar_in1 {
background-color: orange;
}
.bar_in2{
background-color: yellow;
}
.bar_in3{
background-color: brown;
}
.bar_in4{
background-color: chocolate;
}
.bar_in5{
background-color: green;
}
.bar_in6{
background-color: blue;
}
.bar_in7{
background-color: cornflowerblue;
}
.bar_in8{
background-color: deeppink;
}
.bar_in9{
background-color: rgb(171, 204, 23);
}
.bar_in10{
background-color: red;
}
tr{
width:800px;
height: 40px;
}
td{
font-size: 14px;
width: 200px;
line-height: 40px;
border-bottom: 1px solid #ccc;
}
tr #no1{
width: 300px;
}
.header{
font-size: 16px;
font-weight: 700;
}
.t1 {
width: 500px;
}
span{
color:red;
font-size: 14px;
}
</style>
</head>
<body>
<div class="box">
<table>
<tr>
<td colspan="4" class= 'header'>你被“最美鄉(xiāng)村女教師”感動了嗎<span>(必選)</span></td>
</tr>
<tr>
<td class='t1'><input type="checkbox" name="" >很感動,她很美</td>
<td>
<div class="bar">
<div class=" bar_in bar_in1">
</div>
</div>
</td>
<td>0(0%)</td>
</tr>
<tr>
<td class='t1'><input type="checkbox" name="" id="">很感動,她很美</td>
<td>
<div class="bar">
<div class=" bar_in bar_in2">
</div>
</div>
</td>
<td>335733(96.16%)</td>
</tr>
<tr>
<td class='t1'><input type="checkbox" name="" id="">沒感覺,這樣的事很多</td>
<td>
<div class="bar">
<div class="bar_in bar_in3">
</div>
</div>
</td>
<td>4997(1.43%)</td>
</tr>
<tr>
<td class='t1'><input type="checkbox" name="" id="">不感動,可能是炒作</td>
<td>
<div class="bar">
<div class="bar_in bar_in4">
</div>
</div>
</td>
<td>8398(2.41%)</td>
</tr>
</table>
<table>
<tr>
<td colspan="3" class= 'header'>你會愿意為李靈和她的學(xué)校做什么?<span>(必選)</span></td>
</tr>
<tr>
<td class="t1"><input type="checkbox" name="" id="" >捐書給他們,讓他們有個閱覽室</td>
<td>
<div class="bar">
<div class=" bar_in bar_in5">
</div>
</div>
</td>
<td>163002(45.89%)</td>
</tr>
<tr>
<td><input type="checkbox" name="" id="">捐錢給他們,讓他們修繕學(xué)校</td>
<td>
<div class="bar">
<div class="bar_in bar_in6">
</div>
</div>
</td>
<td>52692(15.09%)</td>
</tr>
<tr>
<td><input type="checkbox" name="" id="">向朋友講述李靈的故事</td>
<td>
<div class="bar">
<div class="bar_in bar_in7">
</div>
</div>
</td>
<td>118533(33.96%)</td>
</tr>
<tr>
<td><input type="checkbox" name="" id="">什么都不會做</td>
<td>
<div class="bar">
<div class="bar_in bar_in8">
</div>
</div>
</td>
<td>14881(4.26%)</td>
</tr>
<tr>
<td><input type="checkbox" name="" id="">什么都不會做</td>
<td>
<div class="bar">
<div class="bar_in bar_in9">
</div>
</div>
</td>
<td>0(0%)</td>
</tr>
<tr>
<td><input type="checkbox" name="" id="">什么都不會做</td>
<td>
<div class="bar">
<div class="bar_in bar_in10">
</div>
</div>
</td>
<td>0(0%)</td>
</tr>
</table>
</div>
<script>
var input = document.querySelectorAll('input');
var barin = document.querySelectorAll('.bar_in');
var w = [10,98,30,25,50,22,38,30,34,20,20];
console.log(typeof(5+'%'));
console.log(barin);
console.log(input);
for(var i =0;i<input.length;i++){
input[i].setAttribute('index',i)
input[i].onclick = function(){
var index = this.getAttribute('index')
barin[index].style.width= w[index]+'%';
}
}
</script>
</body>
</html>
實現(xiàn)效果為:

到此這篇關(guān)于JavaScript 排他思想的具體實現(xiàn)的文章就介紹到這了,更多相關(guān)JavaScript 排他內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解微信小程序(Taro)手動埋點和自動埋點的實現(xiàn)
這篇文章主要介紹了詳解微信小程序(Taro)手動埋點和自動埋點的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
ES6中的迭代器、Generator函數(shù)及Generator函數(shù)的異步操作方法
這篇文章主要介紹了ES6中的迭代器、Generator函數(shù)以及Generator函數(shù)的異步操作方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
js change,propertychange,input事件小議
github上關(guān)于mootools一個issue的討論很有意思,所以就想測試記錄下。感興趣的可以點擊原頁面看看2011-12-12
js獲取html的span標(biāo)簽的值方法(超簡單)
下面小編就為大家?guī)硪黄猨s獲取html的span標(biāo)簽的值方法(超簡單)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07
JavaScript中的 attribute 和 jQuery中的 attr 方法淺析
這篇文章主要介紹了JavaScript中的 attribute 和 jQuery中的 attr 方法淺析,需要的朋友可以參考下2017-01-01

