Bootstrap:如何进行下拉导航父链接活动链接?

fmz*_*fmz 10 navigation wordpress twitter-bootstrap twitter-bootstrap-3

我在WP安装上运行Bootstrap,并且从父下拉导航项中删除了url的问题.

这是代码.在menu-item-72中,您可以看到我们团队的href只是#而不是正确的链接.

<ul id="menu-primary" class="nav navbar-nav">
 <li id="menu-item-69" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-69"><a title="Home" href="http://mostellar.opteradev.com/">Home</a></li>
 <li id="menu-item-70" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-6 current_page_item menu-item-70 active"><a title="About Us" href="http://mostellar.opteradev.com/us/">About Us</a></li>
 <li id="menu-item-72" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-72 dropdown"><a title="Our Team" href="#" data-toggle="dropdown" class="dropdown-toggle" aria-haspopup="true">Our Team <span class="caret"></span></a>
  <ul role="menu" class=" dropdown-menu">
    <li id="menu-item-71" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-71"><a title="Katherine M. Conwell, CPA" href="http://mostellar.opteradev.com/katherine-m-conwell/">Katherine M. Conwell, CPA</a></li>
    <li id="menu-item-73" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-73"><a title="Ann S. Bowers, CPA" href="http://mostellar.opteradev.com/our-team/ann-s-bowers/">Ann S. Bowers, CPA</a></li>
    <li id="menu-item-74" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-74"><a title="John B. Mostellar, CPA" href="http://mostellar.opteradev.com/our-team/john-b-mostellar/">John B. Mostellar, CPA</a></li>
    <li id="menu-item-75" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-75"><a title="Lewis T. Shreve, CPA" href="http://mostellar.opteradev.com/our-team/lewis-t-shreve/">Lewis T. Shreve, CPA</a></li>
 </ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

有什么不足以让它发挥作用?我已确认该项目与活动条目相关联.

gig*_*ith 38

默认情况下,下拉列表中的引导父项不可单击.将此脚本添加到您的页面,现在它们将是:

<script>
jQuery(function($) {
$('.navbar .dropdown').hover(function() {
$(this).find('.dropdown-menu').first().stop(true, true).delay(250).slideDown();

}, function() {
$(this).find('.dropdown-menu').first().stop(true, true).delay(100).slideUp();

});

$('.navbar .dropdown > a').click(function(){
location.href = this.href;
});

});
</script>
Run Code Online (Sandbox Code Playgroud)

这个解决方案归功于http://wpeden.com/tipsntuts/twitter-bootstrap-dropdown-on-hover-and-activating-click-event-on-parent-item/


Soh*_*shi 31

对我来说它的工作方式是这样的:我假设您使用了wp-bootstrap-navwalker

用编辑器打开wp-bootstrap-navwalker.php并查找大约行.

// If item has_children add atts to a.
if ( $args->has_children && $depth === 0 ) {
   $atts['href']        = '#';
   $atts['data-toggle'] = 'dropdown';
   $atts['class']           = 'dropdown-toggle';
} else {
   $atts['href'] = ! empty( $item->url ) ? $item->url : '';
}
Run Code Online (Sandbox Code Playgroud)

将这段代码更改为:

// If item has_children add atts to a.
if ( $args->has_children && $depth === 0 ) {
   $atts['href'] = ! empty( $item->url ) ? $item->url : '';
   //$atts['data-toggle']   = 'dropdown';
   $atts['class']           = 'dropdown-toggle';
} else {
   $atts['href'] = ! empty( $item->url ) ? $item->url : '';
}
Run Code Online (Sandbox Code Playgroud)

注意:$ att ['href']现在启用,$ atts ['data-toggle']被禁用,这使得父链接可以点击.

现在打开你的style.css并添加这段代码来激活你的WordPress菜单的悬停功能,带有下拉菜单和可点击的父项.

.dropdown:hover .dropdown-menu {
    display: block;
}
Run Code Online (Sandbox Code Playgroud)

注意:在具有小屏幕的小型设备上,菜单的行为会略有改变.不需要额外的jQuery.

  • 这是我正在寻找的确切答案. (2认同)

ksg*_*ksg 10

您可以通过删除data-toggle ="dropdown"并设置data-hover ="dropdown"来设置.