Bootstrap导航栏汉堡包正在打开但没有关闭

时间:2021-12-01 10:38:28

On a phone sized screen, when I click the hamburger once, the navbar opens fine. When I try to close it, nothing happens - it's stuck. Where have I gone wrong? Here's my html:

在手机大小的屏幕上,当我点击一次汉堡包时,导航栏打开正常。当我试图关闭它时,没有任何反应 - 它被卡住了。我哪里出错了?这是我的HTML:

        <header class="navbar navbar-inverse navbar-fixed-top">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#teams-navbar-collapse">
                        <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="http://{{base_url}}">My Teams</a>
                </div>
                <nav class="collapse navbar-collapse" id="teams-navbar-collapse">
                    <ul class="nav navbar-nav">
                        {% if user.is_authenticated %}
                        <li>
                            <a href="{% url 'profile_page' user.pk %}">
                                My Profile
                            </a>
                        </li>
                        <li>
                            <a href="{% url 'logout' %}">Logout</a>
                        </li>
                    </ul>
                    {% else %}
                    {% block login_button %}{% endblock %}
                    {% endif %}
                </nav>
            </div>
        </header>

2 个解决方案

#1


1  

I would say this is actually an issue with jQuery Toggle. Please ensure jquery is set to toggle "collapsed". One of the ways bootstrap works is by adding/removing classes to buttons/divs/html elements to display them based on css. For example. Pretty Select dropdowns work by adding "open" to a specific div before or after the select button. Hense you can use jQuery to toggle that open when it's clicked. I hope this helps.

我想说这实际上是jQuery Toggle的一个问题。请确保将jquery设置为切换“折叠”。 bootstrap工作方式之一是通过向buttons / divs / html元素添加/删除类来显示它们基于css。例如。 Pretty Select下拉列表通过在选择按钮之前或之后向特定div添加“open”来工作。 Hense你可以使用jQuery在点击时切换打开。我希望这有帮助。

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#teams-navbar-collapse">

<script type="text/javascript">
$( document ).ready(function() {
    $("button.navbar-toggle").click(function(){
        $("button.navbar-toggle").toggleClass("collapse");
    });
});
</script>

#2


0  

I created a jsfiddle: http://jsfiddle.net/93n6vogp/ using the header tag above. It seems to be working fine so the issue you're facing is css-related. Please provide the css of the page you're encountering this issue on.

我使用上面的标题标签创建了一个jsfiddle:http://jsfiddle.net/93n6vogp/。它似乎工作正常所以你面临的问题是与CSS相关。请提供您遇到此问题的页面的CSS。

<nav class="collapse navbar-collapse" id="teams-navbar-collapse">
                <ul class="nav navbar-nav">
                    {% if user.is_authenticated %}
                    <li>
                        <a href="{% url 'profile_page' user.pk %}">
                            My Profile
                        </a>
                    </li>
                    <li>
                        <a href="{% url 'logout' %}">Logout</a>
                    </li>
                </ul>
                {% else %}
                {% block login_button %}{% endblock %}
                {% endif %}
            </nav>

#1


1  

I would say this is actually an issue with jQuery Toggle. Please ensure jquery is set to toggle "collapsed". One of the ways bootstrap works is by adding/removing classes to buttons/divs/html elements to display them based on css. For example. Pretty Select dropdowns work by adding "open" to a specific div before or after the select button. Hense you can use jQuery to toggle that open when it's clicked. I hope this helps.

我想说这实际上是jQuery Toggle的一个问题。请确保将jquery设置为切换“折叠”。 bootstrap工作方式之一是通过向buttons / divs / html元素添加/删除类来显示它们基于css。例如。 Pretty Select下拉列表通过在选择按钮之前或之后向特定div添加“open”来工作。 Hense你可以使用jQuery在点击时切换打开。我希望这有帮助。

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#teams-navbar-collapse">

<script type="text/javascript">
$( document ).ready(function() {
    $("button.navbar-toggle").click(function(){
        $("button.navbar-toggle").toggleClass("collapse");
    });
});
</script>

#2


0  

I created a jsfiddle: http://jsfiddle.net/93n6vogp/ using the header tag above. It seems to be working fine so the issue you're facing is css-related. Please provide the css of the page you're encountering this issue on.

我使用上面的标题标签创建了一个jsfiddle:http://jsfiddle.net/93n6vogp/。它似乎工作正常所以你面临的问题是与CSS相关。请提供您遇到此问题的页面的CSS。

<nav class="collapse navbar-collapse" id="teams-navbar-collapse">
                <ul class="nav navbar-nav">
                    {% if user.is_authenticated %}
                    <li>
                        <a href="{% url 'profile_page' user.pk %}">
                            My Profile
                        </a>
                    </li>
                    <li>
                        <a href="{% url 'logout' %}">Logout</a>
                    </li>
                </ul>
                {% else %}
                {% block login_button %}{% endblock %}
                {% endif %}
            </nav>