使用锚标记链接打开标签页?

时间:2018-07-04 05:35:05

标签: javascript jquery html css3 tabs

因此,我在页面上有两个带有锚标签的标签-我使用首页菜单项上的锚从首页导航到它们。

我已经使用活动类和aria-expanded =“ true”使用了实际的选项卡,但是无法从标题中获取适当的选项卡。

即: 1.标题菜单->单击#tab2链接 2.加载页面->滚动至锚标记-> tab2已打开(这是我遇到的问题,除非我实际单击tab2按钮,否则选项卡1保持打开状态)

我试图更改所涉及的活动类,但是我对javascript / jquery还是比较陌生,所以我不确定我是否可以做到这一点。另外,这在worpress中,并且标题菜单没有用于其自身元素ID的字段。

==============================

编辑,澄清:我可以使用这些标签,并且可以从标题链接转到页面上的标签,我只需要打开正确的标签即可,具体取决于我从标题中单击的链接

我还尝试了启动.click(#tab2),但脚本无法正常运行。

if(window.location.href.indexOf("tab2") > -1) {
       document.getElementById('tab2').click();
    }

编辑: 标头HTML FOT链接:

<ul class="uk-nav uk-nav-navbar">
        <li class="uk-active">
            <a title="" href="testsite.com/#tab1" >tab 1</a></li>
        <li class="uk-active"><a title="" href="testsite.com/#tab2" 
            class="">tab2</a>
        </li>
    </ul>

标签HTML

    <li class="uk-width-medium-1-4 active" id="tab1" aria-expanded="false">
        <a class="uk-button tab1">tab1</a>
    </li>
    <li class="uk-width-medium-1-4 " id="tab2" aria-expanded="false">
        <a class="uk-button tab2">tab2</a>
    </li>

Javascript:

var ai = document.getElementById(""); [I'm having trouble with this, cause I can't see a way to attach an id to a wordpress menu item]

var ch = document.getElementById("");

ai.onclick = function(){
    document.getElementById('tab1').click();
}

ch.onclick = function(){
    document.getElementById('tab2').click();
}

1 个答案:

答案 0 :(得分:0)

您也可以使用jQuery尝试一下。在下面添加示例代码段,请看一看。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Dynamic Tabs</h2>
  <p>To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a .tab-pane class with a unique ID for every tab and wrap them inside a div element with class .tab-content.</p>

  <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
    <li><a data-toggle="tab" href="#menu1">Menu 1</a></li>
    <li><a data-toggle="tab" href="#menu2">Menu 2</a></li>
    <li><a data-toggle="tab" href="#menu3">Menu 3</a></li>
  </ul>

  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <h3>HOME</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>
    <div id="menu1" class="tab-pane fade">
      <h3>Menu 1</h3>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
    </div>
    <div id="menu2" class="tab-pane fade">
      <h3>Menu 2</h3>
      <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
    </div>
    <div id="menu3" class="tab-pane fade">
      <h3>Menu 3</h3>
      <p>Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
    </div>
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
    var anchorHash = window.location.href.toString();
    if( anchorHash.lastIndexOf('#') != -1 ) {
        anchorHash = anchorHash.substr(anchorHash.lastIndexOf('#'));
        if( $('a[href="'+ anchorHash +'"]').length > 0 ) {
            $('a[href="'+ anchorHash +'"]').trigger('click');
        }
    }
});
</script>
</body>
</html>