首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在HTML中使用Div的选项卡

在HTML中使用Div的选项卡
EN

Stack Overflow用户
提问于 2018-07-31 04:35:56
回答 1查看 1.6K关注 0票数 0

我正在尝试使用DIV创建子选项卡。

我可以使用代码查看选项卡和子选项卡。此外,单击main选项卡也可以正常工作。但不幸的是,子选项卡不起作用,这似乎是代码问题。我正在努力寻找问题所在。任何帮助都将受到高度的感谢。提前谢谢。

代码实际上是基于W3学校w3schools

代码语言:javascript
复制
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 name = '0';

document.getElementById(name).click();
代码语言:javascript
复制
body {
       background-image: url("Images/MainBackground.jpeg");
    background-color: #2980B9;
        background-size: 100% 750px;

  font-family: 'Roboto', sans-serif;
  
}

    
    
    /* Style the tab */
.tab {
    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;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
}

/* Fade in tabs */
@-webkit-keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeEffect {
    from {opacity: 0;}
    to {opacity: 1;}
}

#main {
    width: 70px;
    height: 300px;
    border: 1px solid #c3c3c3;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-align-content: center;
    align-content: center;
}

#main div {
    width: 70px;
    height: 70px;
}
代码语言:javascript
复制
<div class="tab">
     <button class="tablinks" id='0' onclick="openCity(event, 'Sales')">Sales</button> 
    
      <button class="tablinks" id='1' onclick="openCity(event, 'Costs')">Costs</button>
            <button class="tablinks" id='2' onclick="openCity(event, 'Expenses')">Expenses</button>
        <button class="tablinks" id='3' onclick="openCity(event, 'Inventory')">Inventory</button>
  <button class="tablinks" id='4' onclick="openCity(event, 'Products')">Products</button>
    <button class="tablinks" id='5' onclick="openCity(event, 'Customers')">Customers</button> 
   <button class="tablinks" id='6' onclick="openCity(event, 'Performance Reports')">Business Insights</button>
      <button class="tablinks" id='7' onclick="openCity(event, 'Administration')">Administration</button>

  </div>

  
<div id="Products" class="tabcontent">    
  <h3>Products</h3>
  
       
      
       
      </div>
<div id="Sales" class="tabcontent">    
  <h3>Sales</h3>
  
     
       
      <div class="tab">
  <button class="tablinks" onclick="openCity(event, 'SaleDetail')">Sale Detail</button>
  <button class="tablinks" onclick="openCity(event, 'SaleSummary')">Sale Summary</button>
  <button class="tablinks" onclick="openCity(event, 'SaleHistory')">Sale History</button>
</div>

<div id="SaleDetail" class="tabcontent">
 test     
</div>

<div id="SaleSummary" class="tabcontent">
    
test2       
</div>
<div id="SaleHistory" class="tabcontent">
Test3
</div>
       
       
       
       <br>
       
       
       
       
</div>

<div id="Customers" class="tabcontent">
  <h3>Customers</h3>
  
  
    

       </div>

<div id="Costs" class="tabcontent">
  <h3>Costs</h3>
  
    
   
</div>
<div id="Expenses" class="tabcontent">
  <h3>Expenses</h3>
  
  
</div>
<div id="Inventory" class="tabcontent">
  <h3>Inventory</h3>
     
       
      
       
</div>
<div id="PerformanceReports" class="tabcontent">
  <h3>Performance Reports</h3>
Analyze business performance
</div>
       
       
       
       <div id="Administration" class="tabcontent">
  <h3>Administration</h3>
 
       
       
       
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-31 05:16:39

您需要在sales中的三个按钮之后关闭一个div:

代码语言:javascript
复制
    ...
  <button class="tablinks" onclick="openCity(event, 'SaleDetail')">Sale Detail</button>
  <button class="tablinks" onclick="openCity(event, 'SaleSummary')">Sale Summary</button>
  <button class="tablinks" onclick="openCity(event, 'SaleHistory')">Sale History</button>
  </div></div>

你刚刚错过了一个

代码语言:javascript
复制
   </div>

在这里,稍后将一个放在错误的位置,在放置关于SaleDetail、SaleSummary的内容div之前,您的销售div必须关闭。

更新代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
    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;
}
</style>
</head>
<body>

<div class="tab">
     <button class="tablinks" id='0' onclick="openCity(event, 'Sales')">Sales</button> 

      <button class="tablinks" id='1' onclick="openCity(event, 'Costs')">Costs</button>
            <button class="tablinks" id='2' onclick="openCity(event, 'Expenses')">Expenses</button>
        <button class="tablinks" id='3' onclick="openCity(event, 'Inventory')">Inventory</button>
  <button class="tablinks" id='4' onclick="openCity(event, 'Products')">Products</button>
    <button class="tablinks" id='5' onclick="openCity(event, 'Customers')">Customers</button> 
   <button class="tablinks" id='6' onclick="openCity(event, 'Performance Reports')">Business Insights</button>
      <button class="tablinks" id='7' onclick="openCity(event, 'Administration')">Administration</button>

  </div>


<div id="Products" class="tabcontent">    
  <h3>Products</h3>




      </div>
<div id="Sales" class="tabcontent">    
  <h3>Sales</h3>



      <div class="tab">
  <button class="tablinks" onclick="openCity(event, 'SaleDetail')">Sale Detail</button>
  <button class="tablinks" onclick="openCity(event, 'SaleSummary')">Sale Summary</button>
  <button class="tablinks" onclick="openCity(event, 'SaleHistory')">Sale History</button>
</div>       
</div>

<div id="SaleDetail" class="tabcontent">
 test     
</div>

<div id="SaleSummary" class="tabcontent">

test2       
</div>
<div id="SaleHistory" class="tabcontent">
Test3
</div>



       <br>


<div id="Customers" class="tabcontent">
  <h3>Customers</h3>




       </div>

<div id="Costs" class="tabcontent">
  <h3>Costs</h3>



</div>
<div id="Expenses" class="tabcontent">
  <h3>Expenses</h3>


</div>
<div id="Inventory" class="tabcontent">
  <h3>Inventory</h3>




</div>
<div id="PerformanceReports" class="tabcontent">
  <h3>Performance Reports</h3>
Analyze business performance
</div>



       <div id="Administration" class="tabcontent">
  <h3>Administration</h3>




</div>

<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";
}
</script>

</body>
</html> 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51601923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档