Javascript在下拉式响应菜单栏中不起作用

时间:2017-04-17 04:12:42

标签: navbar responsive menubar

我是html,css和javascript的新手,目前我正在尝试创建自适应导航栏,但出于某种原因,当我点击480px的菜单按钮时,下拉菜单栏不会出现。如果有人能找到解决这个问题的方法,我真的很感激。

$(function() {
  menu = $('nav ul');

  $('#openup').on('click', function(e) {
    e.preventDefault();
    menu.slideToggle();
  });

  $(window).resize(function() {
    var w = $(this).width();
    if (w > 480 && menu.is(':hidden')) {
      menu.removeAttr('responsive_navbar');
    }
  });

  $('nav li').on('click', function(e) {
    var w = $(window).width();
    if (w < 480) {
      menu.slideToggle();
    }
  });
  $('.open-menu').height($(window).height());
});
body {
  font-family: 'PT Sans Narrow', sans-serif;
  background-color: #eee;
  background-image: url("http://i.cubeupload.com/NjsJx8.png");
  margin: 0;
}

h1 {
  font-size: 4em;
  font-family: 'PT Sans Narrow', sans-serif;
  line-height: 1.1;
  text-align: center;
  padding: 10px;
}

p {
  font-size: 1.2em;
  text-align: center;
  padding: 10px;
}

nav {
  height: 40px;
  width: 100%;
  background-color: #333;
  color: #eee;
}

nav ul {
  padding: 0;
  margin: 0;
}

nav li {
  display: inline;
  float: left;
}

nav a {
  display: inline-block;
  width: 100px;
  text-align: center;
  text-decoration: none;
  padding: 10px 0;
  color: #eee;
  text-decoration: none;
}

nav li:hover {
  background-color: #444;
}

nav a#openup {
  display: none;
}

@media screen and (max-width: 480px) {
  h1 {
    font-size: 2.8em;
  }
  nav {
    height: auto;
    border-bottom: 0;
  }
  nav ul {
    display: none;
    height: auto;
  }
  nav li {
    width: 100%;
    float: left;
    position: relative;
  }
  nav a {
    text-align: left;
    width: 100%;
    text-indent: 25px;
    background: #333;
    border-bottom: 1px solid #555;
  }
  nav a:hover {
    background: #444;
  }
  nav a#openup:after {
    content: "|||";
    transform: rotate(-90deg);
    -ms-transform: rotate(-90deg);
    /* IE 9 */
    -webkit-transform: rotate(-90deg);
    /* Safari and Chrome */
    width: 30px;
    height: 30px;
    display: inline-block;
    position: absolute;
    right: 5px;
    top: 20px;
  }
  nav a#openup {
    display: block;
    background-color: #333;
    width: 100%;
    position: relative;
  }
}

.cf:before,
.cf:after {
  content: "";
  display: table;
}

.cf:after {
  clear: both;
}

.cf {
  zoom: 1;
}
<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <title>Responsive Navbar</title>
  <link rel="stylesheet" href="css/responsive_navbar.css">
  <script type="text/javascript" href="js/responsive_navbar.js"></script>
</head>
<header>
  <nav class="cf">
    <ul class="open-menu">
      <li>
        <a href='#'>Item 0</a>
      </li>
      <li>
        <a href='#'>Item 1</a>
      </li>
      <li>
        <a href='#'>Item 2</a>
      </li>
      <li>
        <a href='#'>Item 3</a>
      </li>
    </ul>
    <a href='#' id='openup'>MENU</a>
  </nav>

</header>

2 个答案:

答案 0 :(得分:0)

您需要添加jquery库文件并修复拼写错误(href - &gt; src)。

<head>
        <meta charset="utf-8">
        <title>Responsive Navbar</title>
        <link rel="stylesheet" href="css/responsive_navbar.css">        
        <script type="text/javascript" src="js/responsive_navbar.js"> </script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>    
</head>

修改

<head>
       <meta charset="utf-8">
       <title>Responsive Navbar</title>
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>    
       <link rel="stylesheet" href="css/responsive_navbar.css">        
       <script type="text/javascript" src="js/responsive_navbar.js"> </script>        
</head>

答案 1 :(得分:0)

右键单击您的解决方案,然后单击“管理Nuget包”。在更新部分更新您的jquery pacakage,然后在页面上添加更新的包的参考。