2017-03-20 38 views
0

我在這個網站上發現了很多有用的答案,當我陷入困境時,現在我發現自己失去了,因爲我很新,並且不知道任何javascript。單擊5行時更改單獨div的背景顏色,並單擊另一個單元時重置?

我想添加一個功能到我的網站,將包括5個按鈕,顯示每個按鈕下面一個div(我有這個沒關係切換的代碼,但!!

這將可能聽起來那麼容易你們,但我怎樣才能改變被點擊div和如果他們點擊其他的div之一然後重新設置背景色,這應在每個的5

<style type="text/css"> 
 
.btnmtb{ 
 
margin-right: 5px; 
 
touch-action: manipulation; 
 
cursor: pointer; 
 
background-color: #1E2327; 
 
padding: 20px 0px; 
 
text-align: center; 
 
} 
 

 

 
.btnmtb h1 { 
 
color: #fff; 
 

 
} 
 

 
.btnmtb h4 { 
 
color: #fff; 
 

 
} 
 

 
.btnmtb p { 
 
color: #fff; 
 
font-family: 'Ubuntu', sans-serif; 
 
font-size: 14px; 
 
padding: 0px 20px 0px 20px; 
 

 
} 
 

 
</style> 
 

 
<div class="disciplines"> 
 
<div class="row"> 
 
<div class="btnmtb col-md-2 col-md-offset-1 box1"> 
 
<p><img src="https://charged.bike/image/catalog/demo/symbols/trail-control.png" width="60%"></p> 
 
<h1>Cross Country</h1> 
 
<h4>Hardtail - 100/120mm</h4> 
 
<p>eMountain Bikes designed for light offroad trails</p> 
 
<i class="fa fa-chevron-down"></i> 
 
</div> 
 
<div class="btnmtb col-md-2 box2"> 
 
<p><img src="https://charged.bike/image/catalog/demo/symbols/trail-control.png" width="60%"></p> 
 
<h1>Cross Country</h1> 
 
<h4>FullSus - 100/120mm</h4> 
 
<p>Fast paced eMTB's designed for light trail with rear suspension</p> 
 
<i class="fa fa-chevron-down"></i> 
 
</div> 
 
<div class="btnmtb col-md-2 box3"> 
 
<p><img src="https://charged.bike/image/catalog/demo/symbols/trail-control.png" width="60%"></p> 
 
<h1>Trail</h1> 
 
<h4>FullSus - 140/150mm</h4> 
 
<p>Mid Travel - perfect weapons for the more ambitious eMTB rider</p> 
 
<i class="fa fa-chevron-down"></i> 
 
</div> 
 
<div class="btnmtb col-md-2 box4"> 
 
<p><img src="https://charged.bike/image/catalog/demo/symbols/trail-control.png" width="60%"></p> 
 
<h1>Enduro</h1> 
 
<h4>FullSus - 160/170mm</h4> 
 
<p>eMountain bikes with long travel designed for the toughest terrain</p> 
 
<i class="fa fa-chevron-down"></i> 
 
</div> 
 
<div class="btnmtb col-md-2 box5"> 
 
<p><img src="https://charged.bike/image/catalog/demo/symbols/trail-control.png" width="60%"></p> 
 
<h1>Fatbike</h1> 
 
<h4>Large Tyres - 100/120mm</h4> 
 
<p>The eMountain bikes that look like they have dropped out of a comic book</p> 
 
<i class="fa fa-chevron-down"></i> 
 
</div> 
 
</div>
工作

回答

0

請使用JavaScript代碼(更新)

var elements = document.getElementsByClassName('btnmtb'); 
for (var i = 0; i < elements.length; i++) { 
    elements[i].addEventListener('click', function() { 
     for (var j = 0; j < elements.length; j++) { 
      if (elements[j].hasAttribute("style")) { 
      elements[j].setAttribute('style', ''); 
      } 
     } 
     this.style.backgroundColor = "red"; 
    }, false); 
}; 
+0

正是我之後。驚人。非常感謝。 –

+0

嗨Sunil,我剛剛注意到背景色在Safari等移動平臺上沒有取消選擇。任何想法,爲什麼會這樣? –

+0

可以在這裏看到的行動 - > https://charged.bike/ebike-buyers-guides/electric-mountain-bikes –

0

HTML

<body> 
    <div class="container"> 
    <div style="background-color:red;width:50%" class="element active"> 
     Red 
    </div> 
    <div style="background-color:yellow;width:50%" class="element"> 
     Yellow 
    </div> 
    <div style="background-color:blue;width:50%" class="element"> 
     Yellow 
    </div> 
    <div style="background-color:black;width:50%" class="element"> 
     Yellow 
    </div> 
    <div style="background-color:white;width:50%" class="element"> 
     Yellow 
    </div> 
    </div> 
</body> 

的JavaScript + JQuery的

$(".element").on("click",function() 
{ 
    $(".element").removeClass("active"); 

    $(this).addClass("active"); 
}); 

CSS

.active{ 
    background-color:green !important; 
} 

JSFiddle

相關問題