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

140
Vistas
Haga clic en evento activado en un elemento pero no en otro

Necesito alternar la visibilidad de una sección (llamada "newHexagon") haciendo clic alternativamente en dos h2 diferentes, "seeHexagon" y "hideHexagon". Cuando se carga la página, la visibilidad de newHexagon se oculta. Si se hace clic en seeHexagon, se cambia correctamente a "visible". El problema es que cuando se hace clic en hideHexagon, no sucede nada, en lugar de que la visibilidad vuelva a estar oculta como lo requiere mi código. El clic en hideHexagon se recibe correctamente, como lo demuestra console.log('clicked'). La siguiente línea de código simplemente parece no tener efecto. Mi guion es el siguiente

 // retrieve element that when clicked fires the click event const seeHexagon = document.getElementById('creaServizi'); // extract necessary html from a data attribute of body const htmlServices = document.body.getAttribute("data-esagonoServizi"); // select the node to which append the new section const servicesSide = document.querySelector('.terzaRigaDes'); // create the new section with visibility hidden const newHexagon = document.createElement('section'); newHexagon.setAttribute('class', 'esagono esagonoServizi'); newHexagon.setAttribute('id', 'sezioneServizi'); newHexagon.style.visibility = 'hidden'; // fill the new section with html extracted above newHexagon.innerHTML = htmlServices; // append the new section to the selected node servicesSide.appendChild(newHexagon); // now that the section is present, retrieve the element that when clicked must fire the second click event let hideHexagon = document.getElementById('chiudiServizi'); // add listener for click event to element that sets the new section's visibility to visible seeHexagon.addEventListener('click', function () { newHexagon.style.visibility = 'visible'; hideHexagon.addEventListener('click', function () { console.log('clicked'); // EVERYTHING WORKS UP TO THIS LINE INCLUDED // NEXT LINE IS INEFFECTIVE AND THAT'S THE PROBLEM newHexagon.style.visibility = 'hidden'; }); });

Tenga en cuenta que se obtiene exactamente el mismo resultado si la segunda función de escucha de eventos se mueve fuera y debajo de la función del primer clic.

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

0

Debe llevar el detector de eventos oculto fuera del otro detector de eventos; de lo contrario, seguirá agregando detectores de eventos. (aparte de eso, el código está bien)

 // retrieve element that when clicked fires the click event const seeHexagon = document.getElementById('creaServizi'); // extract necessary html from a data attribute of body const htmlServices = "hello world"; // select the node to which append the new section const servicesSide = document.querySelector('.terzaRigaDes'); // create the new section with visibility hidden const newHexagon = document.createElement('section'); newHexagon.setAttribute('class', 'esagono esagonoServizi'); newHexagon.setAttribute('id', 'sezioneServizi'); newHexagon.style.visibility = 'hidden'; // fill the new section with html extracted above newHexagon.innerHTML = htmlServices; // append the new section to the selected node servicesSide.appendChild(newHexagon); // now that the section is present, retrieve the element that when clicked must fire the second click event let hideHexagon = document.getElementById('chiudiServizi'); // add listener for click event to element that sets the new section's visibility to visible seeHexagon.addEventListener('click', function() { newHexagon.style.visibility = 'visible'; }); hideHexagon.addEventListener('click', function() { console.log('clicked'); // EVERYTHING WORKS UP TO THIS LINE INCLUDED // NEXT LINE IS INEFFECTIVE AND THAT'S THE PROBLEM newHexagon.style.visibility = 'hidden'; });
 <button id="creaServizi">seeHexagon</button> <button id="chiudiServizi">hideHexagon</button> <div class="terzaRigaDes"></div>

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