两组标签页(Tabs)按钮交互冲突导致内容区域异常空白的问题求助
两组标签页(Tabs)按钮交互冲突导致内容区域异常空白的问题求助
兄弟我太懂这种卡两天找不到问题的憋屈了!让我帮你把这个问题捋明白~
你的问题根源
现在的Tabs函数是全局遍历所有.tabcontent和.tablinks元素,这就导致了两个致命问题:
- 当你点击第二组(Profile里的About/Extra)按钮时,函数会把所有带
.tabcontent类的元素都隐藏——包括外层的profile这个标签内容区!而你要显示的about是嵌套在profile里面的,父容器都被设为display:none了,子元素自然啥都看不到。 - 另外你有两个元素用了同一个
id="defaultOpen",HTML里id是唯一标识,重复的id会导致JS获取元素时出现不可预料的错误,这也是隐性bug。
修复方案:给两组标签页做分组隔离
我们可以给两组标签页的内容和按钮分别设置不同的类名,让Tabs函数只处理当前组的元素,而不是全局所有。
修改后的完整代码:
<div id="wrapper"> <div class="tab"> <button class="tablinks" onclick="Tabs(event, 'profile', 'tabcontent', 'tablinks')" id="defaultOpen">Profile</button> <button class="tablinks" onclick="Tabs(event, 'ref', 'tabcontent', 'tablinks')">Ref Sheet</button> </div> <div id="main"> <div id="content"> <div id="profile" class="tabcontent"> <div id="profileimage"></div> <div class="tab2" id="buttons2"> <!-- 修复重复id,改成defaultOpen2 --> <button class="tablinks2" onclick="Tabs(event, 'about', 'tabcontent2', 'tablinks2')" id="defaultOpen2">About</button> <button class="tablinks2" onclick="Tabs(event, 'extra', 'tabcontent2', 'tablinks2')">Extra</button> </div> <!-- 第二组tabcontent用tabcontent2类 --> <div id="about" class="tabcontent2">hello</div> <div id="extra" class="tabcontent2">这里是额外内容</div> </div> <div id="ref" class="tabcontent"> <div id="references" class="flex-container"> <div class="box"> <img src="https://t3.ftcdn.net/jpg/03/45/05/92/360_F_345059232_CPieT8RIWOUk4JqBkkWkIETYAkmz2b75.jpg"> </div> <div class="box"> <img src="https://t3.ftcdn.net/jpg/03/45/05/92/360_F_345059232_CPieT8RIWOUk4JqBkkWkIETYAkmz2b75.jpg"> </div> <div class="box"> <img src="https://t3.ftcdn.net/jpg/03/45/05/92/360_F_345059232_CPieT8RIWOUk4JqBkkWkIETYAkmz2b75.jpg"> </div> <div class="box"> <img src="https://t3.ftcdn.net/jpg/03/45/05/92/360_F_345059232_CPieT8RIWOUk4JqBkkWkIETYAkmz2b75.jpg"> </div> <div class="box"> <img src="https://t3.ftcdn.net/jpg/03/45/05/92/360_F_345059232_CPieT8RIWOUk4JqBkkWkIETYAkmz2b75.jpg"> </div> <div class="box"> <img src="https://t3.ftcdn.net/jpg/03/45/05/92/360_F_345059232_CPieT8RIWOUk4JqBkkWkIETYAkmz2b75.jpg"> </div> </div> </div> </div> </div> </div>
/* 给第二组tab按钮加样式,和第一组保持视觉统一 */ .tab2 button { background-color: burlywood; width: 8rem; font-size: 18px; border: 3.5px solid brown; border-radius: 10% / 8px; cursor: pointer; color: rgb(176, 103, 66); } .tab2 button:hover { background-color: rgb(191, 147, 89); color: rgb(144, 58, 32); border: 3.5px solid brown; border-radius: 10% / 8px; } .tab2 button:active, .tab2 button.active { background-color: rgb(153, 73, 51); color: rgb(70, 21, 6); border: 2px solid brown; border-radius: 10% / 8px; } /* 第二组tab内容区默认隐藏 */ .tabcontent2 { display: none; padding: 6px 12px; border-top: none; } /* 保留你原来的所有CSS样式 */ #wrapper { width: 100%; height: 100%; display: inline-block; } #main { color: red; background-color: pink; width: 52%; height: 30rem; margin-top: 3rem; margin-inline: auto; border: 6px solid; border-color: brown; border-radius: 15% / 38px; padding: 4px; } #content { color: rgb(0, 0, 0); background-color: beige; width: 96%; height: 27rem; position: relative; top: 1.4rem; margin-inline: auto; border: 4.5px solid brown; border-radius: 15% / 30px; overflow: auto; scrollbar-width: none; } #profileimage { background-color: pink; width: 16rem; height: 25rem; border: 2.5px solid brown; border-radius: 15% / 15px; position: relative; top: 0.4rem; } #references { color: red; } .flex-container { display: flex; flex-wrap: wrap; margin: 0 auto; } .box { flex-grow: 1; flex-shrink: 1; flex-basis: calc(25% - 10px); margin: 5px; } img { width: 100%; } .tab { position: relative; text-align: center; top: 4.5rem; } .tab button { background-color: burlywood; padding: 4px; width: 10rem; font-size: 1.8rem; border: 6px solid brown; border-radius: 10% / 8px; cursor: pointer; color: rgb(176, 103, 66); } .tab button:hover { background-color: rgb(191, 147, 89); color: rgb(144, 58, 32); border: 6px solid brown; border-radius: 10% / 8px; } .tab button:active, .tab button.active { background-color: rgb(153, 73, 51); color: rgb(70, 21, 6); border: 4px solid brown; border-radius: 10% / 8px; } .tabcontent { display: none; padding: 6px 12px; border-top: none; }
// 修改Tabs函数,支持指定要处理的tab内容类和按钮类 function Tabs(evt, tabName, tabContentClass, tabLinksClass) { var i, tabcontent, tablinks; // 只处理当前组的tab内容 tabcontent = document.getElementsByClassName(tabContentClass); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 只处理当前组的tab按钮 tablinks = document.getElementsByClassName(tabLinksClass); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } // 分别触发两组标签的默认选中状态 document.getElementById("defaultOpen").click(); document.getElementById("defaultOpen2").click();
为啥这样改就好了?
- 现在
Tabs函数可以指定要处理的tab内容类和按钮类,点击第二组按钮时,只会隐藏.tabcontent2的元素,不会影响外层的.tabcontent(也就是profile区域)。 - 修复了重复的id,现在两个默认按钮的id唯一,JS可以正确触发它们的点击事件。
- 给第二组按钮加了对应的hover/active样式,和第一组保持视觉统一。
这样你点击About/Extra按钮时,只会切换Profile里面的内容,外层的Profile区域不会被隐藏,自然就不会出现空白了~
备注:内容来源于stack exchange,提问作者kurtis
相关产品推荐
相关产品推荐

