打开带锚链接的标签页

时间:2017-04-05 18:16:44

标签: javascript html

页面末尾有3个标签 我希望顶部的链接一直向下并打开选项卡 什么是最好的方法? 感谢

这是顶部的链接:

<div class="tab">
  <a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 
'Tokyo')"></a>
</div>
<div class="tab2">
  <a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 
'Paris')"></a>
</div>
<div class="tab3">
  <a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 
'London')"></a>
</div>

,这是标签

    <div id="close" class="tabcontent">
</div>

<div id="London" class="tabcontent">  
[insert page='246' display='content']
</div>

<div id="Paris" class="tabcontent">   
[insert page='244' display='content']
</div>

<div id="Tokyo" class="tabcontent">  
[insert page='193' display='content']
</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("close").click();
</script>

感谢

1 个答案:

答案 0 :(得分:0)

您可以尝试更改href链接:

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

单击链接会自动将页面滚动到锚点