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

接下來(lái)我們舉幾個(gè)例子看看吧!
1、實(shí)現(xiàn)簡(jiǎ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>
<!-- 編寫(xiě)一個(gè)完整的tab切換效果的頁(yè)面 -->
<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">第一個(gè)標(biāo)簽的內(nèi)容</div>
<div class="item">第二個(gè)標(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>
實(shí)現(xiàn)效果為:

2、實(shí)現(xiàn)一個(gè)動(dòng)態(tài)點(diǎn)擊的調(diào)查結(jié)果顯示頁(yè)面,要求當(dāng)點(diǎn)擊復(fù)選框選項(xiàng)時(shí)對(duì)應(yīng)的進(jìn)度條增加。
<!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)村女教師”感動(dòng)了嗎<span>(必選)</span></td>
</tr>
<tr>
<td class='t1'><input type="checkbox" name="" >很感動(dòng),她很美</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="">很感動(dòng),她很美</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="">沒(méi)感覺(jué),這樣的事很多</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="">不感動(dòng),可能是炒作</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'>你會(huì)愿意為李靈和她的學(xué)校做什么?<span>(必選)</span></td>
</tr>
<tr>
<td class="t1"><input type="checkbox" name="" id="" >捐書(shū)給他們,讓他們有個(gè)閱覽室</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="">捐錢(qián)給他們,讓他們修繕學(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="">什么都不會(huì)做</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="">什么都不會(huì)做</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="">什么都不會(huì)做</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>
實(shí)現(xiàn)效果為:

到此這篇關(guān)于JavaScript 排他思想的具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JavaScript 排他內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Bootstrap實(shí)現(xiàn)的標(biāo)簽頁(yè)內(nèi)容切換顯示效果示例
這篇文章主要介紹了Bootstrap實(shí)現(xiàn)的標(biāo)簽頁(yè)內(nèi)容切換顯示效果,結(jié)合完整實(shí)例形式分析了基于Bootstrap實(shí)現(xiàn)的標(biāo)簽頁(yè)內(nèi)容切換顯示功能相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2017-05-05
TypeScript基本類(lèi)型之typeof和keyof詳解
typeof 在js中typeof是檢查類(lèi)型的,在ts中也有這個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于TypeScript基本類(lèi)型之typeof和keyof的相關(guān)資料,需要的朋友可以參考下2023-04-04
讓你的博文自動(dòng)帶上縮址的實(shí)現(xiàn)代碼,方便發(fā)到微博客上
添加以下代碼到你的博客中: (呵呵,抄襲至lulu Studio http://s8.hk/0itw)2010-12-12
Javascript中call和apply函數(shù)的比較和使用實(shí)例
這篇文章主要介紹了Javascript中call和apply函數(shù)的比較和使用實(shí)例,本文試圖用更加清晰簡(jiǎn)單的思路來(lái)分析解釋這兩個(gè)函數(shù),需要的朋友可以參考下2015-02-02
如何使用pm2快速將項(xiàng)目部署到遠(yuǎn)程服務(wù)器
這篇文章主要介紹了如何使用pm2快速將項(xiàng)目部署到遠(yuǎn)程服務(wù)器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
document.getElementById的一些細(xì)節(jié)
document.getElementById的一些細(xì)節(jié)...2006-09-09
JavaScript實(shí)現(xiàn)復(fù)制圖片功能的方法示例
本文主要介紹了在JavaScript中實(shí)現(xiàn)復(fù)制圖片的方法,先介紹了實(shí)現(xiàn)復(fù)制的前置知識(shí),包括傳統(tǒng)的 execCommand 方法及其優(yōu)缺點(diǎn)和 Clipboard API,然后詳細(xì)闡述了如何將不同形式的圖片轉(zhuǎn)化為blob對(duì)象并通過(guò)Clipboard API實(shí)現(xiàn)復(fù)制,還提及了兼容性問(wèn)題及預(yù)覽、下載圖片的實(shí)現(xiàn)思路2025-03-03
AJAX異步從優(yōu)酷專(zhuān)輯中采集所有視頻及信息(JavaScript代碼)
上次寫(xiě)了一個(gè) .NET從優(yōu)酷專(zhuān)輯中采集所有視頻及信息(VB.NET代碼)2010-11-11

