簡體   English   中英

鏈接到同一網頁中的選項卡式部分

[英]Link to a tabbed section within the same web page

我有這個基本且干凈的小標簽包。 見下文。 HTML,js和CSS文件

我想通過從一個選項卡式部分到另一選項卡式部分的鏈接進行導航。

我嘗試過的唯一解決方案是:

<a id="agree"> location </a> and <a href="#agree"> link </a>

但這不起作用。

我正在提交以下代碼,因此您可以看到我正在使用的代碼

的HTML:

 <div class="tab">
  <button class="tablinks" onclick="openCity(event, 'Welcome')" id="defaultOpen">Welcome</button>
<button class="tablinks" onclick="openCity(event, 'Agreement')" id="defaultOpen">Agreement</button>

 </div>
 <div id="Welcome" class="tabcontent">
  <h3>Welcome</h3>
 </div>
 <div id="Agreement" class="tabcontent">
 </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";
}

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

CSS:

<style>
.tab {
    float: left;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
    width: 15%;
    height: 380px;
font-family: "Lato", sans-serif;
box-sizing: border-box;
}

/* Style the buttons inside the tab */
.tab button {
    display: block;
    background-color: inherit;
    color: black;
    padding: 22px 16px;
    width: 100%;
    border: none;
    outline: none;
    text-align: left;
    cursor: pointer;
    transition: 0.3s;
    font-size: 17px;
box-sizing: border-box;
}

/* Change background color of buttons on hover */
.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current "tab button" class */
.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    float: left;
    padding: 0px 12px;
    border: 1px solid #ccc;
    width: 85%;
    border-left: none;
    height: 380px;
box-sizing: border-box;
}
</style>

我看到您的兩個按鈕具有相同的ID。 切勿對HTML元素使用相同的ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM