S
S
Stanislav Grigoriev2018-12-28 19:11:04
Computer networks
Stanislav Grigoriev, 2018-12-28 19:11:04

How to set several different buttons to open one tab?

<script>
        function openTab(evt, name) {
            // Declare all variables
            var i, tabcontent, tablinks;

            // Get all elements with class="tabcontent" and hide them
            tabcontent = document.getElementsByClassName("tabcontent");
            for (i = 0; i < tabcontent.length; i++) {
                tabcontent[i].style.display = "none";
            }

            // Get all elements with class="tablinks" and remove the class "active"
            tablinks = document.getElementsByClassName("tablinks");
            for (i = 0; i < tablinks.length; i++) {
                tablinks[i].className = tablinks[i].className.replace(" active", "");
            }

            // Show the current tab, and add an "active" class to the button that opened the tab
            document.getElementById(name).style.display = "flex";
            evt.currentTarget.className += " active";

        }
        // Get the element with id="defaultOpen" and click on it
        document.getElementById("defaultOpen").click();

    </script>

In JS, it’s still dumb and doesn’t reach where you need to write additional. button classes. Provided that different buttons with different classes are visually very different.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey S., 2016-03-14
@Winsik

20 Mbps in frames of 1250 bytes
6 Mbps in frames of 125 bytes
... we count how many packets (frames) in each of the options ... we calculate k

S
Stalker_RED, 2018-12-28
@Stalker_RED

tablinks

M
Marat Garifullin, 2018-12-28
@magarif

<ul>
  <li class="tablinks" data-index="1">1</li>
  <li class="tablinks" data-index="2">2</li>
  <li class="tablinks" data-index="3">3</li>
</ul>
<div class="tabcontainer">
  <div class="tabcontent">
    1
  </div>
  <div class="tabcontent">
    2
  </div>
  <div class="tabcontent">
    3
  </div>
</div>

<a href="#" data-index="2">2</a>

function openTab(curIndex) {
            const tabcontent = document.getElementsByClassName('tabcontent');
            tabcontent.forEach((one, index) => {
              one.style.display = index != curIndex ? 'none' : 'flex';
            });

            const tablinks = document.getElementsByClassName('tablinks');
            tablinks.forEach((one, index) => {
              if (index != curIndex) one.classList.remove('active');
              else if (!one.classList.has('active')) one.classList.add('active');
            });
        }

            const links = document.querySelector('[data-index]');
            links.forEach(one => {
              one.addEventListener('click', () => openTab(one.dataset.index));
            });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question