Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
responsive nav bar add class to divelement

i took this code from w3school where create a responsive nav bar. the nav bar add icon, add functionality for icon and change menu display. in js code, nav is assign to div element and nav has 2 case, if has class topnav or else (dont have class topnav). in html, nav has class top nav, so i dont understand what else case is for. else case is needed because when i remove else case functionality also change. please someone explain

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

/* .topnav a.active {
  background-color: #04aa6d;
  color: white;
} */

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  /* only display home */
  .topnav a:not(:first-child) {
    display: none;
  }
  /* display icon dropdown  */
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
    />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="topnav" id="myTopnav">
      <a href="#home">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
      <a href="javascript:void(0);" class="icon" onclick="myFunction()">
        <i class="fa fa-bars"></i>
      </a>
    </div>

    <script>
      function myFunction() {
        //objecthtmldivelement
        var nav = document.getElementById("myTopnav");
        // alert(nav);
        if (nav.className === "topnav") {
          //class responsive position icon to top right
          //and display a as block text float left
          nav.className += " responsive";
        } else {
          nav.className = "topnav";
        }
      }
    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

this function is for /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */

else part is for removing the "responsive" class so that it remain topnav like default it was

about 4 years ago · Juan Pablo Isaza Denunciar

0

nav.className === "topnav" is a exact match statement, so if you click the icon, the nav.className become 'topnav responsive', which is not exact match to 'topnav'(in other words, 'topnav' === 'topnav responsive' is false), this is the functionality that the else block want to handle.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda