ASP.NET WebForms和Bootstrap 4.1.3导航栏不起作用

时间:2019-03-07 02:53:27

标签: html asp.net twitter-bootstrap webforms

我正在尝试学习Bootstrap 4 Navbar,并使其使用框架4.7.2(Visual Studio 2013)与ASP.NET Webform一起使用。我从Bootstraps自己的网站复制了代码,并将其放在母版页中。在中,我有:

        <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link type="text/css" rel="stylesheet" href="../../CSS/bootstrap.min.css" />
    <script type="text/javascript" src="../../JavaScript/jquery-3.3.1.min.js" ></script>
    <script type="text/javascript" src="../../JavaScript/bootstrap.min.js" ></script>
    <script type="text/javascript" src="../../JavaScript/bootstrap.bundle.min.js" ></script>
    <script type="text/javascript" src="../../JavaScript/popper.min.js"></script>

在<身体>中,我有:

<form id="form1" runat="server">

        <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <a class="navbar-brand" href="#">Navbar</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>

          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
              <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Another action</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item" href="#">Something else here</a>
                </div>
              </li>
              <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
              </li>
            </ul>
          </div>
        </nav>
        </div>        

        <asp:ContentPlaceHolder ID="body_content" runat="server" ></asp:ContentPlaceHolder>
    </form>

按照Bootstrap示例,页面呈现良好,但是下拉菜单不执行任何操作。但是,如果我创建一个.HTML页面并粘贴完全相同的代码,则效果很好。所以我想知道为什么这段代码不能在WebForm中工作?

我使用的是更高版本的Bootstrap.JS(4.1.3),最新的jQuery(3.1.1)和最新的popper.js。

谢谢。

1 个答案:

答案 0 :(得分:0)

我发现以下方法有效,即只需单击文本下拉菜单,即可打开菜单。使用您的代码,它可以打开,但是只需单击两次文本即可。

我使用以下代码来测试这种情况。请注意,bootstrap.bundle.min.js包括popper.js以及bootstrap.min.js,因此无需包括这些JavaScript文件。这可能就是为什么它无法按预期工作的原因。

您可以查看以下文档,了解有关bootstrap.bundle.min.js的详细信息:Bootstrap Bundle JavaScript Includes what

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>BootStrap Navbar</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js" integrity="sha256-fzFFyH01cBVPYzl16KT40wqjhgPtq6FFUB6ckN2+GGw=" crossorigin="anonymous"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
                <a class="navbar-brand" href="#">Navbar</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav mr-auto">
                        <li class="nav-item active">
                            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="#">Link</a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown
                            </a>
                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link disabled" href="#">Disabled</a>
                        </li>
                    </ul>
                </div>
            </nav>
        </div>
    </form>
</body>
</html>