jQuery Mobile + Bootstrap - 可折叠导航栏菜单在加载另一页后无法切换

时间:2016-12-08 06:20:56

标签: twitter-bootstrap jquery-mobile menu toggle navbar

我有一个JQM + Bootstrap实现,带有可折叠的导航栏菜单,在应用程序中加载另一个页面后拒绝切换。我假设通过JQM加载的ajax页面干扰了Bootstrap的折叠菜单切换功能。

的index.html

<!DOCTYPE html>
<html>
<head>
    <title>Home - Websites 2 Go</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <!-- JSSOR slideshow -->
    <link rel="stylesheet" href="lib/jssor/jssor.css">

    <script src="lib/jssor/jssor.js"></script>

    <!-- All Cart Items -->
    <script src="js/items.js"></script>
    <script src="js/cart.js"></script>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="css/custom.css">
</head>
<body>

<div data-role="page" id="page">

    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapse-menu" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="cart.html">Cart (0 items)</a></li>
            </ul>
            <a class="navbar-brand" href="index.html">Websites 2 Go</a>
        </div>


        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="collapse-menu">
          <ul class="nav navbar-nav">
            <li class="active"><a href="index.html">Home <span class="sr-only">(current)</span></a></li>
            <li><a href="products.html">Products</a></li>
            <li><a href="services.html">Services</a></li>
            <li><a href="cart.html">Cart</a></li>
            <li><a href="contact.html">Contact</a></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>

    <div data-role="header">
        <h1>Home</h1>
    </div><!-- /header -->


    <!-- Slideshow -->
    ...
    <script type="text/javascript">jssor_1_slider_init();</script>



    <div role="main" class="ui-content">
        <h3 class="ui-bar ui-bar-a">Build & Buy a Website</h3>
        <div class="ui-body">
            ...
            <br><br>
            <a href="getstarted.html" class="btn btn-default">Get Started</a>
        </div>
    </div><!-- /content -->

    <div data-role="footer">
        <h4>Copyright &copy; 2016</h4>
    </div><!-- /footer -->
</div><!-- /page -->

<script>
$(document).on('pageshow', function() {
  $(".navbar-nav li a").click(function(event) {
    $(".navbar-collapse").collapse();
  });
});
</script>

</body>
</html>

我尝试过:

<script>
$(document).on('pageshow', function() {
  $(".navbar-nav li a").click(function(event) {
    $(".navbar-collapse").collapse();
  });
});
</script>


<script>
$(document).on('pageinit', function() {
  $(".navbar-nav li a").click(function(event) {
    $(".navbar-collapse").collapse();
  });
});
</script>


<script>
$(document).on('pageload', function() {
  $(".navbar-nav li a").click(function(event) {
    $(".navbar-collapse").collapse();
  });
});
</script>

1 个答案:

答案 0 :(得分:0)

解决方案是将导航栏移到data-role="page"容器的上方和外部。