【问题标题】:Bootstrap button dropdown not below button引导按钮下拉菜单不在按钮下方
【发布时间】:2019-06-04 04:02:34
【问题描述】:

我在获取引导按钮下拉菜单以实际显示按钮下方的下拉菜单时遇到问题。它目前显示在另一个 div 下方。在过去的几个小时里,我一直试图让它工作,但我做不到。

HTML

    <!DOCTYPE html>
    <html lang="en">

    <head>
      <link href="css/style.css" rel="stylesheet" />
      <script src="js/jquery.min.js"></script>
      <script src="js/bootstrap.min.js"></script>
    </head>

    <body class="nav-md">
      <div class="container body">
        <div class="main_container">
          <div class="col-md-3 left_col">
            <div class="left_col scroll-view"> </div>
          </div>
          <!-- page content -->
          <div class="right_col" role="main">
            <div class="">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_content">
                    <div class="" data-example-id="togglable-tabs" role="tabpanel">
                      <ul class="nav nav-tabs bar_tabs" id="myTab" role="tablist">
                        <li class="active" role="presentation"> <a aria-expanded="true" data-toggle="tab" href="#tab_content1" id="home-tab" role="tab"><i class="fa fa-globe"></i> Tab 1</a></li>
                      </ul>
                      <div class="tab-content" id="myTabContent">
                        <div aria-labelledby="home-tab" class="tab-pane fade active in" id="tab_content1" role="tabpanel">
                          <div class="row">
                            <div id="saveloaddiv" style="float:right;display:inline-block;"> <button data-toggle="dropdown" class="btn btn-success dropdown-toggle btn-xs" type="button" aria-expanded="true">Success <span class="caret"></span>
                                        </button>
                              <ul role="menu" class="dropdown-menu">
                                <li><a href="#">Action</a> </li>
                                <li><a href="#">Another action</a> </li>
                                <li><a href="#">Something else here</a> </li>
                                <li class="divider"></li>
                                <li><a href="#">Separated link</a> </li>
                              </ul>
                            </div>
                          </div>
                          <div class="clearfix"></div>
                          <div class="row">
                            <div class="col-md-12 col-sm-12 col-xs-12">
                              <div id="text">
                                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- /page content -->
        </div>
      </div>
    </body>
    </html>

我正在为我的 css 和 js 使用 twitter bootstrap cdn。

我做了一个小提琴来说明问题:https://jsfiddle.net/rdawkins/f7m6cv7q/8/

【问题讨论】:

    标签: jquery html css twitter-bootstrap


    【解决方案1】:

    解决办法的关键是position: relative;。

    将按钮和下拉菜单包装在div 中,并创建容器position: relative;。通过 CSS 将菜单放在按钮下方。

    请参阅an updated JSFiddle 和下面的额外代码。

    HTML

    <div class="dropdown-container"><!--Container wrapped around-->
      <button data-toggle="dropdown" class="btn btn-success dropdown-toggle btn-xs" type="button" aria-expanded="true">Success <span class="caret"></span>
      </button>
      <ul role="menu" class="dropdown-menu">
        <li><a href="#">Action</a> </li>
        <li><a href="#">Another action</a> </li>
        <li><a href="#">Something else here</a> </li>
        <li class="divider"></li>
        <li><a href="#">Separated link</a> </li>
      </ul>
    </div><!--Container closed-->
    

    CSS

    .dropdown-container {
      position: relative;
    }
    
    .dropdown-menu {
      position: absolute;
      top: 100%; /* Bottom of button */
      right: 0;
      margin-left: -100px; /* More to the left */
    }
    

    【讨论】:

    • 完美运行!非常感谢罗伊! :) 我现在将更多地了解与position 的区别。干杯
    • 很高兴我能帮上忙,Mozilla Developer Network 是学习定位元素的绝佳起点。
    • 谢谢,我去看看!
    • 为我工作。答案发布 3 年后为我节省了很多时间。
    • 我在 Firefox 73.0.1 和 google chrome 80 上都试过了,但它不再工作了。谁能帮忙??!!
    猜你喜欢
    • 2018-07-26
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多