简体   繁体   English

Dropddown标题链接到锚点

[英]Dropddown header links to anchor

I want my dropdown menu header to both open the related menu and show directly the content of the first element of the submenu which is actually an anchor link in the page. 我希望我的下拉菜单标题打开相关菜单并直接显示子菜单的第一个元素的内容,该元素实际上是页面中的锚链接。 Here is the HTML code of the Dropdown Menu: 以下是下拉菜单的HTML代码:

 <div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav nav-pills nav-stacked">     
   <li class="dropdown">
      <a href="#" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown">Menu</a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
          <li><a href="#submenu1" tabindex="-1" data-toggle="tab">submenu1</a></li>
          <li><a href="#submenu2" tabindex="-1" data-toggle="tab">submenu2</a></li>
        </ul>
    </li> 
  </ul>
</div>  

This is the HTML code of the anchored link: 这是锚定链接的HTML代码:

<div id="myNavbar" class="tab-content">
        <div class="tab-pane fade" id="submenu1">
          <p>submenu1</p>
        </div>
</div>

And this is the JS code I'm trying to use with no success. 这是我试图使用的JS代码没有成功。 It works if I write a whole URL like "www.google.com" but not with "#submenu1". 如果我写一个像“www.google.com”这样的整个网址而不是“#submenu1”,它就可以工作。

$(document).ready(function() {
  $('.dropdown').on('click', function () {
   window.location="#submenu1";
  });
});

Try using the window.location.hash instead? 请尝试使用window.location.hash吗?

window.location.hash=anchorname;

Edit 编辑

You're using Twitter bootstrap right? 你正在使用Twitter引导程序吗? The tabs won't be visible until the active class is added, and since you have a fade class too, I think you'll also need the 'in' class... Try this JS : 在添加活动类之前,选项卡将不可见,并且由于您也有淡入淡出类,我认为您还需要'in'类...尝试此JS

$('.dropdown').on('click', function () {
  // Remove any active classes from the tabs
  $('.tab-pane').removeClass('active in');
  // Set the specific #submenu1 to be active
  $('#submenu1').addClass('active in');
  // Scroll the window down to the tabs that are now visible
  window.location.href="#submenu1";
});

I'm not entirely sure what you're trying to achieve though, the links in the dropdown should do the displaying for you... but if you're just trying to force a specific tab to be open when you first click the nav dropdown, then this should do the trick. 我不完全确定你想要实现的是什么,下拉列表中的链接应该为你显示...但是如果你只是想在第一次点击导航时强制打开一个特定的标签下拉,然后这应该做的伎俩。

you can try with 你可以试试

location.hash=anchorname 的location.hash = anchorname

location.hash = "Submenu15";
var x = "The anchor " + location.hash;
document.getElementById("demo").innerHTML = x;

fiddle link http://jsfiddle.net/420rL0h2/ 小提琴链接http://jsfiddle.net/420rL0h2/

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

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