[英]Changing active tab colour when using a previous/next button
I have a page where I am using W3 CSS tabs to open different pages by months. 我有一个页面,我在其中使用W3 CSS标签按月打开不同的页面。 When the tabs are clicked the relative page opens and the tab turns to red to show it is active.
单击选项卡后,将打开相对页面,并且选项卡将变为红色以表明它处于活动状态。 I have added to the bottom of the pages previous/next month buttons.
我已在上一页/下个月按钮的页面底部添加了内容。 When clicked these work fine and take you to the previous or next month but the tab does not change colour at the top of the page to show it is active.
单击这些按钮后,效果很好,您可以转到上一个月或下个月,但是该选项卡不会更改页面顶部的颜色以表明它处于活动状态。 How can I get the tab bar to also change colour when the buttons are used instead of the tab bar?
当使用按钮而不是标签栏时,如何使标签栏也改变颜色?
This is the tab bar code: 这是标签条形码:
<div class="w3-bar w3-black" >
<button class="w3-bar-item w3-button tablink w3-red" onclick="openNews(event,'MAY2018')">TOUKOKUU</button>
<button class="w3-bar-item w3-button tablink" onclick="openNews(event,'JUNE2018')">KESÄKUU</button>
<button class="w3-bar-item w3-button tablink" onclick="openNews(event,'JULY2018')">HEINÄKUU</button>
<button class="w3-bar-item w3-button tablink" onclick="openNews(event,'AUGUST2018')">ELOKUU</button>
<button class="w3-bar-item w3-button tablink" onclick="openNews(event,'SEPTEMBER2018')">SYYSKUU</button>
Buttons are : 按钮是:
<div class="w3-section" align="center">
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large " onclick="openNews(event,'MAY2018'), topFunction()" >❮ TOUKOKUU</button>
<button class="w3-btn w3-white w3-border w3-border-red w3-round-large " onclick="openNews(event,'JULY2018'), topFunction()">HEINÄKUU ❯</button>
</div>
The JavaScript is : JavaScript是:
function openNews(evt, newsName) {
var i, x, tablinks;
x = document.getElementsByClassName("news");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < x.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" w3-red", "");
}
document.getElementById(newsName).style.display = "block";
evt.currentTarget.className += " w3-red";
}
I changed your code a bit. 我更改了您的代码。 See the code snippet below:
请参见下面的代码段:
function openNews( newsName ) { var x = document.querySelectorAll( '.news' ), tablinks = document.querySelectorAll( '.tablink' ), index = 0; document.querySelector( '.w3-red' ).classList.remove( 'w3-red' ); document.querySelector( '.active' ).classList.remove( 'active' ); for ( var i = 0; i < x.length; i++ ) { if ( x[ i ].id == newsName ) index = i } x[ index ].classList.add( 'active' ); tablinks[ index ].classList.add( 'w3-red' ) }
.tablink { transition-duration: 400ms } .news { display: none; border-top: 6px solid #f44336 !important } .active { display: block }
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <div class="w3-container"> <div class="w3-bar w3-black" > <button class="w3-bar-item w3-button tablink w3-red" onclick="openNews('MAY2018')">TOUKOKUU</button> <button class="w3-bar-item w3-button tablink" onclick="openNews('JUNE2018')">KESÄKUU</button> <button class="w3-bar-item w3-button tablink" onclick="openNews('JULY2018')">HEINÄKUU</button> <button class="w3-bar-item w3-button tablink" onclick="openNews('AUGUST2018')">ELOKUU</button> <button class="w3-bar-item w3-button tablink" onclick="openNews('SEPTEMBER2018')">SYYSKUU</button> </div> <div id="MAY2018" class="w3-container w3-border news active"> <h2>MAY 2018</h2> <p>News for TOUKOKUU 2018</p> </div> <div id="JUNE2018" class="w3-container w3-border news"> <h2>JUNE 2018</h2> <p>News for KESÄKUU 2018</p> </div> <div id="JULY2018" class="w3-container w3-border news"> <h2>JULY 2018</h2> <p>News for HEINÄKUU 2018</p> </div> <div id="AUGUST2018" class="w3-container w3-border news"> <h2>AUGUST 2018</h2> <p>News for ELOKUU 2018</p> </div> <div id="SEPTEMBER2018" class="w3-container w3-border news"> <h2>SEPTEMBER 2018</h2> <p>News for SYYSKUU 2018</p> </div> </div> <div class="w3-section" align="center"> <button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="openNews('MAY2018')" >❮ TOUKOKUU</button> <button class="w3-btn w3-white w3-border w3-border-red w3-round-large" onclick="openNews('JULY2018')">HEINÄKUU ❯</button> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.