Bootstrap 3 Drop Up菜单包含子菜单

时间:2015-05-04 22:14:17

标签: html css drop-down-menu jquery-plugins twitter-bootstrap-3

使用jquery 1.9.1.js,下面显示的html,css和javascript与Bootstrap 3.3.4完美配合。当它使用查询1.10.2.js放置在运行Bootstrap 3.0.0的页面中时,菜单会打开,但是当单击带有子菜单的项目时,菜单会折叠。有什么建议?感谢。

HTML

  <div class="col-sm-4">          <h3 class="header-title">Navigation</h3>
      <span class="line"></span>      <div class="dropup m-b">            <button
     

class =&#34; btn btn-success&#34;类型=&#34;按钮&#34;数据肘节=&#34;下拉&#34;&GT;菜单                               

  • 行动
  • 另一项行动         下拉标题
  • 子操作
  • 另一个子操作
  • 此处有其他内容
  • 此处还有其他内容
  •         
  • 分隔链接
  • CSS

      

    .dropdown-submenu&gt; a:在{content:&#34;&#34 ;; float:right;
      margin-right:-10px; } @media(min-width:768px){。dropdown-submenu   {       位置:相对; } .dropdown-submenu .dropdown-menu {       顶部:0;       左:100%;       margin-top:-6px;       border-top-left-radius:0; .dropup .dropdown-submenu .dropdown-menu,.navbar-fixed-bottom .dropdown-submenu   。下拉式菜单 {       顶部:汽车;       底部:0;       margin-top:0;       margin-bottom:-6px;       border-top-left-radius:4px;       border-bottom-left-radius:0; }。dropdown-menu-right .dropdown-submenu .dropdown-menu {       左:汽车;       对:100%;       border-top-left-radius:4px;       border-top-right-radius:0; } .dropup .dropdown-menu-right .dropdown-submenu .dropdown-menu,.navbar-fixed-bottom   .dropdown-menu-right .dropdown-submenu .dropdown-menu {       border-radius:4px 4px 0; } .dropdown-submenu&gt; a:在{之后       margin-top:6px;       border-left:4px虚线;       border-top:4px实心透明;       border-bottom:4px实心透明; } .dropdown-menu-right .dropdown-submenu&gt; a:在{之后       向左飘浮;       border-left:none;       margin-left:-10px;       margin-right:0;       border-right:4px虚线;       border-top:4px实心透明;       border-bottom:4px实心透明; @media(max-width:767px){。dropdown-submenu .dropdown-menu {       位置:静态;       margin-top:0;       边界:0;       box-shadow:none; } .dropdown-submenu&gt; a:在{之后       margin-top:8px;       border-top:4px虚线;       border-left:4px实心透明;       border-right:4px实心透明; } .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt;   li.dropdown-header,.dropup&gt; .dropdown-menu&gt; .dropdown-submenu&gt;   .dropdown-menu&gt; li.dropdown-header,.btn-group&gt; .dropdown-menu&gt;   .dropdown-submenu&gt; .dropdown-menu&gt; li.dropdown-header,.dropdown&gt;   .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li>一,
      .dropup&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li>   a,.btn-group&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown菜单

         
        

    li&gt;一个 {         padding-left:30px; } .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt;     li.dropdown-header,.dropup&gt; .dropdown-menu&gt; .dropdown-submenu&gt;     .dropdown-menu&gt; li> .dropdown-menu&gt; li.dropdown头,
        .btn-group&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt;里     .dropdown-menu&gt; li.dropdown-header,.dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>一,
        .dropup&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li>     .dropdown-menu&gt; li> a,.btn-group&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>一个 {         padding-left:40px; } .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li.dropdown-header,.dropup&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li.dropdown-header,.btn-group&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li.dropdown-header,.dropdown&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li> a,.dropup&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li> a,.btn-group&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li>一个 {         padding-left:50px; } .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li> .dropdown-menu&gt; li.dropdown-header,.dropup&gt;     .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li>     .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt;     li.dropdown-header,.btn-group&gt; .dropdown-menu&gt; .dropdown,子菜单     .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li.dropdown-header,.dropdown&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li> .dropdown-menu&gt; li> a,.dropup&gt;     .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li>     .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li>一,
        .btn-group&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt;里     .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li>一个 {         padding-left:60px; } .navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li.dropdown-header,.navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt;     .dropdown-menu&gt; li>一个 {         padding-left:35px; } .navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li.dropdown-header,.navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt;     .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li>一个 {         padding-left:45px; } .navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li.dropdown-header,.navbar-nav&gt; .dropdown&gt;     .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li>     .dropdown-menu&gt; li> .dropdown-menu&gt; li>一个 {         padding-left:55px; } .navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt; .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li.dropdown头,
        .navbar-nav&gt; .dropdown&gt; .dropdown-menu&gt; .dropdown-submenu&gt;     .dropdown-menu&gt; li> .dropdown-menu&gt; li> .dropdown-menu&gt; li>     .dropdown-menu&gt; li>一个 {         padding-left:65px; }}

      

    的jQuery

      

    &#39;使用严格的&#39;;

         

    (function(factory){if(typeof define ==&#39; function&#39;&amp;&amp; define.amd)   {       // AMD注册为匿名模块       define([&#39; jquery&#39;],工厂); } else if(typeof exports ==&#39; object&#39;){       // Node / CommonJS       module.exports = factory(require(&#39; jquery&#39;)); } else {       //浏览器全局变量       工厂(jQuery的); ()(函数($){var desc =&#39;:not(.disabled,.divider,.dropdown-header)&#39;;

         

    function Submenupicker(element){       这个。$ element = $(element);       这个。$ main = this。$ element.closest(&#39; .dropdown,.dropup,.btn-group&#39;);       这个。$ menu = this。$ element.parent();       这个。$ drop = this。$ menu.parent()。parent();       这个。$ menus = this。$ menu.siblings(&#39; .dropdown-submenu&#39;);

    var $children = this.$menu.find('> .dropdown-menu > ' + desc);
    
    this.$submenus = $children.filter('.dropdown-submenu');
    this.$items = $children.not('.dropdown-submenu');
    
    this.init();   }
    
         

    Submenupicker.prototype = {       init:function(){         这个。$ element.on(&#39; click.bs.dropdown&#39;,this.click.bind(this));         这$ element.keydown(this.keydown.bind(本));         这个。$ menu.on(&#39; hide.bs.submenu&#39;,this.hide.bind(this));         。这$ items.keydown(this.item_keydown.bind(本));

      // Bootstrap fix
      this.$menu.nextAll(desc + ':first:not(.dropdown-submenu)').children('a').keydown(this.next_keydown.bind(this));
    },
    click: function(event) {
      event.stopPropagation();
    
      this.toggle();
    },
    toggle: function() {
      if (this.$menu.hasClass('open')) {
        this.close();
      }
      else {
        this.$menu.addClass('open');
        this.$menus.trigger('hide.bs.submenu');
      }
    },
    hide: function(event) {
      // Stop event bubbling
      event.stopPropagation();
    
      this.close();
    },
    close: function() {
      this.$menu.removeClass('open');
      this.$submenus.trigger('hide.bs.submenu');
    },
    keydown: function(event) {
      // 13: Return, 27: Esc, 32: Spacebar
      // 38: Arrow up, 40: Arrow down
    
      // Off vertical scrolling
      if (/^(32|38|40)$/.test(event.keyCode)) {
        event.preventDefault();
      }
    
      if (/^(13|32)$/.test(event.keyCode)) {
        this.toggle();
      }
      else if (/^(27|38|40)$/.test(event.keyCode)) {
        event.stopPropagation();
    
        if (event.keyCode == 27) {
          if (this.$menu.hasClass('open')) {
            this.close();
          }
          else {
            this.$menus.trigger('hide.bs.submenu');
            this.$drop.removeClass('open').children('a').focus();
          }
        }
        else {
          var $items = this.$main.find('li:not(.disabled):visible > a');
    
          var index = $items.index(event.target);
    
          if (event.keyCode == 38 && index !== 0) {
            index--;
          }
          else if (event.keyCode == 40 && index !== $items.length - 1) {
            index++;
          }
          else {
            return;
          }
    
          $items.eq(index).focus();
        }
      }
    },
    item_keydown: function(event) {
      // 27: Esc
    
      if (event.keyCode != 27) {
        return;
      }
    
      event.stopPropagation();
    
      this.close();
      this.$element.focus();
    },
    next_keydown: function(event) {
      // 38: Arrow up
    
      if (event.keyCode != 38) {
        return;
      }
    
      // Off vertical scrolling
      event.preventDefault();
    
      event.stopPropagation();
    
      // Use this.$drop instead this.$main (optimally)
      var $items = this.$drop.find('li:not(.disabled):visible > a');
    
      var index = $items.index(event.target);
    
      $items.eq(index - 1).focus();
    }   };
    
         

    //对于AMD / Node / CommonJS使用的元素(可选)//   http://learn.jquery.com/jquery-ui/environments/amd/返回   ($ .fn.submenupicker = function(elements){       var $ elements =这个instanceof $?这个:$(元素);

    return $elements.each(function() {
      var data = $.data(this, 'bs.submenu');
    
      if (!data) {
        data = new Submenupicker(this);
    
        $.data(this, 'bs.submenu', data);
      }
    });   }); });
    

    1 个答案:

    答案 0 :(得分:0)

    弄清楚出了什么问题。我没有把脚本放入。这是以防万一有人想尝试这个。

       $(&#39; .dropdown-submenu&gt; a&#39;)。submenupicker();