【问题标题】:Bootstrap: How do I make Dropdown navigation Parent links an active link?Bootstrap:如何使下拉导航父链接成为活动链接?
【发布时间】:2014-10-30 19:10:29
【问题描述】:

我在 WP 安装上运行 Bootstrap,并且遇到从父下拉导航项中剥离 url 的问题。

这里是代码。在 menu-item-72 中,您可以看到 our-team 的 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>

为了让它发挥作用,缺少什么?我已确认该项目与活动条目相关联。

【问题讨论】:

    标签: wordpress twitter-bootstrap twitter-bootstrap-3 navigation


    【解决方案1】:

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

    <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>
    

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

    【讨论】:

    • 这对于延迟响应很有用。把它传下去真是太好了。什么会导致 URL 不存在以及如何恢复?
    • @fmz 抱歉,我误解了你最初的问题。我需要知道更多。你能提供一个链接到你的问题吗?你用的是什么 Bootstrap WP 主题?
    • 这是一个自定义主题。你可以在这里查看:mostellar.opteradev.com。如果您查看 Our Team,您会发现它没有 URL,只有 #,但它和其他页面一样是常规页面条目。
    • @fmz 抱歉,看过之后,我不知道。我最好的未受过教育的猜测:) 鉴于 twitter-bootstrap 的默认设置不允许下拉列表中的父可点击链接,那么主题本身在我无权访问的 php 文件中有一些东西,自动剥离那些链接?但是如果你自己做主题,那我就不知道了。祝你在其余的解决方案上好运。 . .
    • 感谢您的最大努力。既然你解决了点击能力部分,我仍然会给你功劳。
    【解决方案2】:

    对我来说它是这样工作的:我假设你使用 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 : '';
    }
    

    把这段代码改成:

    // 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 : '';
    }
    

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

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

    .dropdown:hover .dropdown-menu {
        display: block;
    }

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

    【讨论】:

    • 这是我正在寻找的确切答案。
    • 非常棒,非常感谢:)
    • 小型设备有什么已知的解决方案吗?
    • 您也可以将 $atts['data-toggle'] = 'dropdown' 更改为 $atts['data-hover'] = 'dropdown' 在这种情况下,您无需更改 css 中的任何内容。
    • 这个解决方案非常完美,真的帮了我很大的忙。当您的菜单超过一个子级时,只需添加一件事,您需要通过添加 ">" .dropdown:hover > .dropdown-menu { display: block; 来稍微修改 CSS。 }
    【解决方案3】:

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

    【讨论】:

    • 感谢您的意见。
    【解决方案4】:

    为了添加到 Sohail Qureshi 的解决方案,我对文件进行了更多修改,这是一种将父链接转换为实际链接的方法:

    在 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 : '';
    }
    

    到:

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

    现在父链接是可点击的,实际上链接到页面!

    【讨论】:

      【解决方案5】:

      2018 年 5 月,这个解决方案对我有用。

      转到您的 class-wp-bootstrap-navwalker.php 文件 -> 注释第 185 行

      // $atts['href']          = '#';
      

      并粘贴此代码。

      $atts['href'] = ! empty( $item->url ) ? $item->url : '';
      $atts['data-toggle']   = 'hover';
      

      享受吧。

      【讨论】:

        【解决方案6】:

        易于使用以下代码在下拉菜单中点击链接

        onClick="parent.location='http://www.plus2net.com/'"
        

        【讨论】:

          【解决方案7】:

          //哇!!!此代码工作正常。只需将其粘贴到项目的正确位置即可。 // 如果 item has_children 添加 atts 到 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 : '';
          }
          

          【讨论】:

          • 如果existing answer 有帮助,添加评论即可,无需根据其片段做出新的回答。
          猜你喜欢
          • 2013-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-15
          • 2020-03-25
          • 1970-01-01
          • 2012-04-10
          • 2020-09-11
          相关资源
          最近更新 更多