Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

html - JavaScript for tabs stops working when tabs are repeated on page with different id's

I am trying to build a gallery that will showcase my responsive design projects. I want to have one accordion for each project which expands to reveal two tabs: (1) the desktop version and (2) the mobile version. The tab for the desktop version will be open by default.

Liveweave - Working Tabs

Screenshot - Desktop Tab Open

Screenshot - Mobile Tab Open

I grabbed code for the accordions and tabs from w3schools, which works perfectly for the first project. However, when I add more projects (more accordions and tabs), the tabs stop functioning properly.

I do not know JavaScript too well so can someone please help me?

Here is the HTML for the accordion and tabs:

<button class="accordion">Project 1</button>
    
        <div class="panel">
        
            <div class="tab">
    
                <button class="tablinks" onclick="openPreview(event, 'Desktop')" id="defaultOpen">Desktop</button><button class="tablinks" onclick="openPreview(event, 'Mobile')">Mobile</button>

            </div>

            <div id="Desktop" class="tabcontent">
            
                <div class="preview-desktop-container">

                    <div class="preview-desktop">
            
                        <img src="https://placehold.it/600x900.jpg" width="100%" height="auto" alt="Desktop Version">
                
                    </div>
                
                </div>
                
            </div>
            
            <div id="Mobile" class="tabcontent">
            
                <div class="preview-mobile-container">
            
                    <div class="preview-mobile">
                
                        <img src="https://placehold.it/320x1800.jpg" width="100%" height="auto" alt="Mobile Version">
                    
                    </div>
                
                </div>

            </div>
        
        </div>

And here is the JavaScript:

<script>

// Accordions
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
  });
}

// Open the first accordion
  var firstAccordion = acc[0];
  var firstPanel = firstAccordion.nextElementSibling;
  firstAccordion.classList.add("active");
  firstPanel.style.maxHeight = "100%";

// Tabs
function openPreview(evt, deviceName) {
  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(deviceName).style.display = "block";
  evt.currentTarget.className += " active";
}

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

</script>

As I said, this works fine for one project. When I add more projects (by duplicating the first project and using it as a template), the tabs stop working. Please see below.

Liveweave - Broken Tabs

Screenshot - Broken Tabs

Here is the HTML for the broken tabs:

    <button class="accordion">Project 1</button>
    
        <div class="panel">
        
            <div class="tab">
    
                <button class="tablinks" onclick="openPreview(event, 'Desktop')" id="defaultOpen1">Desktop</button><button class="tablinks" onclick="openPreview(event, 'Mobile')">Mobile</button>

            </div>

            <div id="Desktop1" class="tabcontent">
            
                <div class="preview-desktop-container">

                    <div class="preview-desktop">
            
                        <img src="https://placehold.it/600x900.jpg" width="100%" height="auto" alt="Desktop Version">
                
                    </div>
                
                </div>
                
            </div>
            
            <div id="Mobile1" class="tabcontent">
            
                <div class="preview-mobile-container">
            
                    <div class="preview-mobile">
                
                        <img src="https://placehold.it/320x1800.jpg" width="100%" height="auto" alt="Mobile Version">
                    
                    </div>
                
                </div>

            </div>
        
        </div>
        
    <button class="accordion">Project 2</button>
    
        <div class="panel">     
        
            <div class="tab">
    
                <button class="tablinks" onclick="openPreview(event, 'Desktop')" id="defaultOpen2">Desktop</button><button class="tablinks" onclick="openPreview(event, 'Mobile')">Mobile</button>

            </div>

            <div id="Desktop2" class="tabcontent">
            
                <div class="preview-desktop-container">

                    <div class="preview-desktop">
            
                        <img src="https://placehold.it/600x900.jpg" width="100%" height="auto" alt="Desktop Version">
                
                    </div>
                
                </div>
                
            </div>
            
            <div id="Mobile2" class="tabcontent">
            
                <div class="preview-mobile-container">
            
                    <div class="preview-mobile">
                
                        <img src="https://placehold.it/320x1800.jpg" width="100%" height="auto" alt="Mobile Version">
                    
                    </div>
                
                </div>

            </div>
        
        
        </div>
            
    <button class="accordion">Project 3</button>
        
        <div class="panel">
                        
            <div class="tab">
    
                <button class="tablinks" onclick="openPreview(event, 'Desktop')" id="defaultOpen3">Desktop</button><button class="tablinks" onclick="openPreview(event, 'Mobile')">Mobile</button>

            </div>

            <div id="Desktop3" class="tabcontent">
            
                <div class="preview-desktop-container">

                    <div class="preview-desktop">
            
                        <img src="https://placehold.it/600x900.jpg" width="100%" height="auto" alt="Desktop Version">
                
                    </div>
                
                </div>
                
            </div>
            
            <div id="Mobile3" class="tabcontent">
            
                <div class="preview-mobile-container">
            
                    <div class="preview-mobile">
                
                        <img src="https://placehold.it/320x1800.jpg" width="100%" height="auto" alt="Mobile Version">
                    
                    </div>
                
                </div>

            </div>
                
        </div>

As you can see, there are 3 elements which have id's:

button id="defaultOpen"
div id="Desktop"
div id="Mobile"

When I duplicated the first project, I added numbers after the id's to make them unique. For example, I changed "defaultOpen" to "defaultOpen1". I think this must be conflicting with the JavaScript, but I don't know what the problem is or how to fix it!

I would really appreciate somebody's help. Please let me know if I need to provide additional information. Thank you in advance... and sorry for the length of this post!

question from:https://stackoverflow.com/questions/65839554/javascript-for-tabs-stops-working-when-tabs-are-repeated-on-page-with-different

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...