<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-family: Arial; margin: 0; } /* Style the tab */ .tab { width: 100%; margin: auto; position: fixed; margin-top: 0em; overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ .tab button.active { // background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } .tabcontent2 { display: block; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } </style> </head> <body> <div class="tab"> <button class="tablinks1" onclick="openCity(event, 'London')">London</button> <button class="tablinks2" onclick="openCity(event, 'Paris')">Paris</button> <button class="tablinks3" onclick="openCity(event, 'Tokyo')">Tokyo</button> <button class="tablinks4" onclick="openCity(event, 'Warszawa')">Warszawa</button> </div> <div id="London" class="tabcontent"><br><br><br> <h3>London</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"><br><br><br> <h3>Paris</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"><br><br><br> <h3>Tokyo</h3> <p>Tokyo is the capital of Japan.</p> </div> <div id="Warszawa" class="tabcontent"><br><br><br> <h3>Warszawa</h3> <p>Warszawa is the capital of Poland.</p> </div> <div class="tabcontent2"> <br><br><br><br>Ten tekst ma zostać ukryty jeśli zostanie wciśnięty jeden z przycisków z powyższego MEN </div> <script> function openCity(evt, cityName) { var i, tabcontent, tabcontent2, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); tabcontent2 = document.getElementsByClassName("tabcontent2"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } </body> </html>
Próbowałem jak ponizej ale coś nie działa
<script> function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; var x = document.getElementsByClassName("tabcontent2"); if (tabcontent[i].style.display === "block") { x.style.display = "none"; } else { x.style.display = "none"; } } </script>