繁体   English   中英

在HTML中使用Div的标签

[英]Tabs using Div in HTML

我正在尝试使用DIV创建子选项卡。
我可以使用代码查看选项卡和子选项卡。 同样单击主选项卡正在工作。 但是不幸的是,子选项卡不起作用,这似乎是代码问题。 我正在努力寻找问题。 任何帮助将不胜感激。 提前致谢。

代码实际上基于W3schools W3schools

 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(); 
 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; } 
 <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> 

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

    ...
  <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>

在此放置一个错误的位置,以后,必须关闭Sales div,然后再放置有关SaleDetail,SaleSummary的内容div。

更新的代码:

<!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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM