基于w3学校教程的响应式导航栏无法使用javascript打开

时间:2017-10-10 12:22:51

标签: javascript html css

这可能是非常简单的,因为html,css和js中的代码行少于90行,但我不明白为什么它不起作用。

它仍然大部分仍然正确格式化,但是关键问题是单击按钮似乎没有运行javascript来将类更改为响应,或者如果确实如此,则响应类不起作用。

此处提供实时示例https://jsfiddle.net/hh5brhfk/1/

HTML:

function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
/* styling for the responsive nav bar */

.topnav .icon {
  display: none; /* Hide the link that should open and close the topnav on small screens */
}

@media screen and (max-width:959px) {
  /* Add a black background color to the top navigation */
  .topnav {
    background-color: #333;
    overflow: hidden;
  }

  .topnav ul {
    list-style-type: none;
    margin-top:0px; 
  }

  /* Style the links inside the navigation bar */
  .topnav a {
    float: left;
    display: block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
  }

  /* Change the color of links on hover */
  .topnav a:hover {
    background-color: #ddd;
    color: black;
  }
}
/* When the screen is less than 959 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */
@media screen and (max-width: 959px) {
  .topnav li:not(:first-child) {display: none;}
  .topnav li.icon {
    float: right;
    display: block;
  }
}

/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 959px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive li {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive li a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="topnav" id="nav-container">
    <nav>
      <ul>
      <li><a href="index.php">Home</a></li>
        <li><a href="about.php">About Us</a></li>
        <li><a href="whatweteach.php">What we teach</a></li>
        <li><a href="gallery.php">Gallery</a></li>
        <li><a href="contact.php">Contact Us</a></li>
        <li class="icon"><a href="javascript:void(0);"  onclick="myFunction()">&#9776;</a></li>
      </ul>
    </nav>
  </div>

<div style="padding-left:16px">
  <h2>Responsive Topnav Example</h2>
  <p>Resize the browser window to see how it works.</p>
</div>

3 个答案:

答案 0 :(得分:2)

只是一些变化。

首先,在JSFiddle中,您需要将JavaScript设置加载类型更改为No wrap - in <head>

enter image description here

Here's a SO answer ,了解为什么需要在JSFiddle中执行此操作。

然后注意您正在查询不存在的ID myTopnav

相反,您可以查询现有的topnav类,如下所示:

var x = document.querySelector(".topnav");

试试下面的代码:

function myFunction() {
  var x = document.querySelector(".topnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
/* styling for the responsive nav bar */

.topnav .icon {
  display: none;
  /* Hide the link that should open and close the topnav on small screens */
}

@media screen and (max-width:959px) {
  /* Add a black background color to the top navigation */
  .topnav {
    background-color: #333;
    overflow: hidden;
  }
  .topnav ul {
    list-style-type: none;
    margin-top: 0px;
  }
  /* Style the links inside the navigation bar */
  .topnav a {
    float: left;
    display: block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
  }
  /* Change the color of links on hover */
  .topnav a:hover {
    background-color: #ddd;
    color: black;
  }
}


/* When the screen is less than 959 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */

@media screen and (max-width: 959px) {
  .topnav li:not(:first-child) {
    display: none;
  }
  .topnav li.icon {
    float: right;
    display: block;
  }
}


/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */

@media screen and (max-width: 959px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive li {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive li a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="topnav" id="nav-container">
  <nav>
    <ul>
      <li><a href="index.php">Home</a></li>
      <li><a href="about.php">About Us</a></li>
      <li><a href="whatweteach.php">What we teach</a></li>
      <li><a href="gallery.php">Gallery</a></li>
      <li><a href="contact.php">Contact Us</a></li>
      <li class="icon"><a href="javascript:void(0);" onclick="myFunction()">&#9776;</a></li>
    </ul>
  </nav>
</div>

<div style="padding-left:16px">
  <h2>Responsive Topnav Example</h2>
  <p>Resize the browser window to see how it works.</p>
</div>

答案 1 :(得分:0)

如果其他人有同样的问题,则是由于缺少div标签造成的。

<div id="nav-container">
<div class="topnav" id="myTopnav">

答案 2 :(得分:0)

您使用myTopnav来获取getElementById,但在您的代码中没有这个。已使用nav-container

function myFunction() {
    var x = document.getElementById("nav-container");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
/* styling for the responsive nav bar */

.topnav .icon {
  display: none; /* Hide the link that should open and close the topnav on small screens */
}

@media screen and (max-width:959px) {
  /* Add a black background color to the top navigation */
  .topnav {
    background-color: #333;
    overflow: hidden;
  }

  .topnav ul {
    list-style-type: none;
    margin-top:0px; 
  }

  /* Style the links inside the navigation bar */
  .topnav a {
    float: left;
    display: block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
  }

  /* Change the color of links on hover */
  .topnav a:hover {
    background-color: #ddd;
    color: black;
  }
}
/* When the screen is less than 959 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */
@media screen and (max-width: 959px) {
  .topnav li:not(:first-child) {display: none;}
  .topnav li.icon {
    float: right;
    display: block;
  }
}

/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 959px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive li {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive li a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="topnav" id="nav-container">
    <nav>
      <ul>
      <li><a href="index.php">Home</a></li>
        <li><a href="about.php">About Us</a></li>
        <li><a href="whatweteach.php">What we teach</a></li>
        <li><a href="gallery.php">Gallery</a></li>
        <li><a href="contact.php">Contact Us</a></li>
        <li class="icon"><a href="javascript:void(0);"  onclick="myFunction()">&#9776;</a></li>
      </ul>
    </nav>
  </div>

<div style="padding-left:16px">
  <h2>Responsive Topnav Example</h2>
  <p>Resize the browser window to see how it works.</p>
</div>