idTabs - 在标签

时间:2016-05-20 09:00:49

标签: javascript jquery tabs

我正在使用idTabs来控制页面上的标签内容。 我在顶部有标签菜单,下面是标签内容。 在第一个选项卡式内容中,我有一个锚点,当点击它时,需要切换到标签号2.这是示例HTML:

<div id="practice-menu">
    <ul class="tabs">
        <li><a href="#practice-info-content">Practice information</a></li>
        <li><a href="#practice-treatment-content">Our treatments</a></li>
        <li><a href="#practice-team-content">Meet the team</a></li>
    </ul>
</div>

<div id="practice-info-content" class="tab-content">
    <h2 class="section-title">Practice information</h2>
    <a href="#practice-treatment-content">See our treatments and costs</a>
</div>

<div id="practice-treatment-content" class="tab-content">
    <h2 class="section-title">Our treatments and costs</h2>
</div>

<div id="practice-team-content" class="tab-content">
    <h2 class="section-title">Meet the team</h2>
</div>

这是一个标签的小提琴,锚点已经到位,但它没有切换标签:https://jsfiddle.net/uLy39osc/

我搜索并搜索过,无法找到适用于idTabs的答案。有谁知道这是如何工作的?

2 个答案:

答案 0 :(得分:0)

    <!DOCTYPE html>
    <html>
    <style>
    body {font-family: "Lato", sans-serif;}

    ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
    }

     /* Float the list items side by side */
    .tab li {float: left;}

    /* Style the links inside the list items */
    ul.tab li a {
    display: inline-block;
    color: black;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
    }

    /* Change background color of links on hover */
    ul.tab li a:hover {
    background-color: #ddd;
    }

    /* Create an active/current tablink class */
    ul.tab li a:focus, .active {
    background-color: #ccc;
    }

    /* Style the tab content */
    .tabcontent {
    display: none;
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-top: none;
    }
    </style>
    <body>

    <p>Click on the links inside the tabbed menu:</p>

    <ul class="tab">
    <li><a href="#" class="tablinks" onclick="openCity(event, 'London')">London</a></li>
    <li><a href="#" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a></li>
    <li><a href="#" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a></li>
    </ul>

    <div id="London" class="tabcontent">
    <h3>London</h3>
    <p>London is the capital city of England.</p>
    </div>

    <div id="Paris" class="tabcontent">
    <h3>Paris</h3>
     <p>Paris is the capital of France.</p> 
     </div>

    <div id="Tokyo" class="tabcontent">
     <h3>Tokyo</h3>
     <p>Tokyo is the capital of Japan.</p>
    </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 < tabcontent.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}
</script>

</body>
</html> 

答案 1 :(得分:0)

好的,所以设法让这个工作。 所以基本上我在选项卡内容中的锚点上添加了一个click事件,它接受了href,将其与选项卡菜单的href匹配,然后触发了对该锚点的点击。效果很好!

$('.tab-switch').click(function(event) {
    event.preventDefault();
    var hash = $(this).attr("href");
    if (hash) $('.tabs li a[href$="'+hash+'"]').trigger('click');
});