简体   繁体   English

使用Div的jQuery Toggle导航栏

[英]JQuery Toggle Navigation Bar Using Div

So I'm trying to make my navigation bar toggle using JQuery however when I click on the span button, nothing is happening. 因此,我试图使用JQuery来切换导航栏,但是当我单击span按钮时,什么也没有发生。

HTML 的HTML

       <div id="navbar">
        <span class="navbar-btn"></span>
        <ul>
            <li class="currentpage"><a href="/Unit20/Photographer/">Home</a></li>
            <li><a href="#">Blog</a></li>
            <li><a href="#">Portfolio</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
      </div>

JQuery jQuery查询

    <script>
        $('span.navbar-btn').click(function() {
            $('#navbar').toggle();
        });
    </script>

Live Version can be found at http://joshscottonthe.net/Unit20/Photographer - Just rescale your browser less than 960 pixels and you should see the button 实时版本可在http://joshscottonthe.net/Unit20/Photographer上找到-只需将浏览器缩放到小于960像素,您应该会看到该按钮

You need to include your script after the document is loaded. 加载文档后,需要包括脚本。

$(function() {
  $('span.navbar-btn').click(function() {
    $('#navbar').toggle();
  });
})

Or you can include it in the same way you did, just make sure that the <script> tag is placed after that <span class='navbar-button'> . 或者,您也可以采用与以前相同的方式包含它,只需确保将<script>标记放置在该<span class='navbar-button'>

I think this should solve the problem: 我认为这应该可以解决问题:

 $(document).ready(function(){
            $('span.navbar-btn').click(function() {
                $('#navbar').toggle();
            });
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM