Bootstrap下拉列表在点击时不适用于移动设备

时间:2019-06-10 07:09:02

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3

我已在我的网站上添加了一个下拉列表,但是当导航栏折叠时,它在移动设备上无法正常工作。当我点击下拉菜单时,它会关闭导航栏,而不显示下拉菜单下的项目。

spread
library(dplyr)
library(tidyr)

df %>%
  mutate(row = row_number()) %>%
  gather(key, value, -row) %>%
  group_by(row) %>%
  mutate(value = replace(value, duplicated(value), NA)) %>%
  spread(key, value) %>%
  ungroup %>%
  select(-row)

# A tibble: 3 x 3
#  label val   val1 
#  <chr> <chr> <chr>
#1 a     x     z    
#2 b     NA    NA   
#3 c     NA    d    

2 个答案:

答案 0 :(得分:0)

这是您要找的东西吗? Navbar with sub-menu

(function($) {
  $(document).ready(function() {
    $('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
      event.preventDefault();
      event.stopPropagation();
      $(this).parent().siblings().removeClass('open');
      $(this).parent().toggleClass('open');
    });
  });
})(jQuery);
.dropdown-submenu {
  position: relative;
}

.dropdown-submenu>.dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -6px;
  margin-left: -1px;
  -webkit-border-radius: 0 6px 6px 6px;
  -moz-border-radius: 0 6px 6px 6px;
  border-radius: 0 6px 6px 6px;
}

.dropdown-submenu>a:after {
  display: block;
  content: " ";
  float: right;
  width: 0;
  height: 0;
  border-color: transparent;
  border-style: solid;
  border-width: 5px 0 5px 5px;
  border-left-color: #cccccc;
  margin-top: 5px;
  margin-right: -10px;
}

.dropdown-submenu:hover>a:after {
  border-left-color: #555;
}

.dropdown-submenu.pull-left {
  float: none;
}

.dropdown-submenu.pull-left>.dropdown-menu {
  left: -100%;
  margin-left: 10px;
  -webkit-border-radius: 6px 0 6px 6px;
  -moz-border-radius: 6px 0 6px 6px;
  border-radius: 6px 0 6px 6px;
}


/*
@media (min-width: 768px) { 
}
@media (min-width: 992px) { 
}
@media (min-width: 1200px) { 
}
*/
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>

<body>
  <div class="container pen">
    <div class="row">
      <div class="col-sm-12">

        <nav class="navbar navbar-default" role="navigation">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            <a class="navbar-brand" href="#" target="_blank">Brand</a>
          </div>

          <div class="collapse navbar-collapse" id="navbar-collapse-1">
            <ul class="nav navbar-nav">
              <li class="active"><a href="#">Active Link</a></li>
              <li><a href="#">Link</a></li>
              <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                <ul class="dropdown-menu">
                  <li><a href="#">Dropdown 1</a></li>
                  <li><a href="#">Dropdown 2</a></li>
                  <li><a href="#">Dropdown 3</a></li>
                  <li class="divider"></li>
                  <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown 4</a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Dropdown Submenu 4.1</a></li>
                      <li><a href="#">Dropdown Submenu 4.2</a></li>
                      <li><a href="#">Dropdown Submenu 4.3</a></li>
                      <li><a href="#">Dropdown Submenu 4.4</a></li>
                    </ul>
                  </li>
                  <li class="dropdown dropdown-submenu">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown 5</a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Dropdown Submenu 5.1</a></li>
                      <li><a href="#">Dropdown Submenu 5.2</a></li>
                      <li><a href="#">Dropdown Submenu 5.3</a></li>
                      <li class="divider"></li>
                      <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Submenu 5.4</a>
                        <ul class="dropdown-menu">
                          <li><a href="#">Dropdown Submenu 5.4.1</a></li>
                          <li><a href="#">Dropdown Submenu 5.4.2</a></li>
                          <li class="divider"></li>
                          <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Submenu 5.4.3</a>
                            <ul class="dropdown-menu">
                              <li><a href="#">Dropdown Submenu 5.4.3.1</a></li>
                              <li><a href="#">Dropdown Submenu 5.4.3.2</a></li>
                              <li><a href="#">Dropdown Submenu 5.4.3.3</a></li>
                              <li><a href="#">Dropdown Submenu 5.4.3.4</a></li>
                            </ul>
                          </li>
                          <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Submenu 5.4.4</a>
                            <ul class="dropdown-menu">
                              <li><a href="#">Dropdown Submenu 5.4.4.1</a></li>
                              <li><a href="#">Dropdown Submenu 5.4.4.2</a></li>
                              <li><a href="#">Dropdown Submenu 5.4.4.3</a></li>
                              <li><a href="#">Dropdown Submenu 5.4.4.4</a></li>
                            </ul>
                          </li>
                        </ul>
                      </li>
                    </ul>
                  </li>
                </ul>
              </li>
              <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                <ul class="dropdown-menu">
                  <li><a href="#">Dropdown Link 1</a></li>
                  <li><a href="#">Dropdown Link 2</a></li>
                  <li><a href="#">Dropdown Link 3</a></li>
                  <li class="dropdown dropdown-submenu">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Link 4</a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Dropdown Submenu Link 4.1</a></li>
                      <li><a href="#">Dropdown Submenu Link 4.2</a></li>
                      <li><a href="#">Dropdown Submenu Link 4.3</a></li>
                      <li><a href="#">Dropdown Submenu Link 4.4</a></li>
                    </ul>
                  </li>
                  <li class="dropdown dropdown-submenu">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Link 5</a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Dropdown Submenu Link 5.1</a></li>
                      <li><a href="#">Dropdown Submenu Link 5.2</a></li>
                      <li><a href="#">Dropdown Submenu Link 5.3</a></li>
                      <li class="divider"></li>
                      <li class="dropdown dropdown-submenu">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Submenu Link 5.4</a>
                        <ul class="dropdown-menu">
                          <li><a href="#">Dropdown Submenu Link 5.4.1</a></li>
                          <li><a href="#">Dropdown Submenu Link 5.4.2</a></li>
                          <li class="divider"></li>
                          <li class="dropdown dropdown-submenu">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Submenu Link 5.4.3</a>
                            <ul class="dropdown-menu">
                              <li><a href="#">Dropdown Submenu Link 5.4.3.1</a></li>
                              <li><a href="#">Dropdown Submenu Link 5.4.3.2</a></li>
                              <li><a href="#">Dropdown Submenu Link 5.4.3.3</a></li>
                              <li><a href="#">Dropdown Submenu Link 5.4.3.4</a></li>
                            </ul>
                          </li>
                          <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Submenu Link 5.4.4</a>
                            <ul class="dropdown-menu">
                              <li><a href="#">Dropdown Submenu Link 5.4.4.1</a></li>
                              <li><a href="#">Dropdown Submenu Link 5.4.4.2</a></li>
                              <li><a href="#">Dropdown Submenu Link 5.4.4.3</a></li>
                              <li><a href="#">Dropdown Submenu Link 5.4.4.4</a></li>
                            </ul>
                          </li>
                        </ul>
                      </li>
                    </ul>
                  </li>
                </ul>
              </li>
            </ul>
          </div>
          <!-- /.navbar-collapse -->
        </nav>

      </div>
    </div>
  </div>
</body>

</html>

答案 1 :(得分:0)

您的scriptlink标签在代码段中放置错误。这些标记应放在您的 HTML 文件中。链接标记必须放置在head标记中。 我以为您犯了一个错误,对您的问题进行了修改,但是将这些标记放在它们所属的位置可以达到预期的结果:

.dropdown-submenu {
  position: relative;
}

.dropdown-submenu .dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

<nav class="navbar navbar-default navbar-fixed-top">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#page-top">Name</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li class="hidden">
                    <a href="#page-top"></a>
                </li>
                <li class="page-scroll">
                    <a href="#portfolio">Portfolio</a>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Games <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="/Spikes" target="_blank">Avoid The Spikes</a></li>
                        <li><a href="/SpaceShooter" target="_blank">Space Shooter</a></li>
                        <!--<li role="separator" class="divider"></li>-->
                        <!--<li class="dropdown-header">Nav header</li>-->
                        <!--<li><a href="#">Separated link</a></li>-->
                    </ul>
                </li>
                </ul>
                </div>
</div>