Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
HTML, JavaScript. Cómo mostrar información diferente en el mismo botón de ventana emergente

Me gustaría saber cómo puedo mostrar información diferente en las mismas ventanas emergentes.

Estoy usando este ejemplo: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal

Descubrí que para activar algunos de los mismos botones, tengo que editar el código JavaScript, convertir los botones en clases.

 <button class="myBtn">butoon 1</button> <button class="myBtn">button 2</button> <button class="myBtn">button 3</button>

JavaScript:

 // Get the modal var modal = document.getElementById('myModal'); // Get the button that opens the modal var btns = document.getElementsByClassName("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks the button, open the modal for (var i = 0; i < btns.length; i++) { btns[i].onclick = function() { modal.style.display = "block"; } } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }

Pero si creo algunos botones iguales y cambio la información, obtengo la misma información que en la primera ventana emergente. ¿Como puedo resolver esto?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Usa este código.
Funciona bien para mí.

 var modal = document.getElementById("myModal"); // Get the button that opens the modal var btns = document.getElementsByClassName("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; var modalText = document.getElementById('modal-text'); // When the user clicks the button, open the modal for (var i = 0; i < btns.length; i++) { btns[i].onclick = function(event) { switch(event.target.innerHTML) { case "button 1": modalText.innerHTML = "First button Clicked"; break; case "button 2": modalText.innerHTML = "Second button Clicked"; break; default: modalText.innerHTML = "Third button Clicked"; } modal.style.display = "block"; } } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }
 body {font-family: Arial, Helvetica, sans-serif;} /* The Modal (background) */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 100px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content */ .modal-content { background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; width: 80%; } /* The Close Button */ .close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; }
 <button class="myBtn">button 1</button> <button class="myBtn">button 2</button> <button class="myBtn">button 3</button> <!-- The Modal --> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">&times;</span> <p id="modal-text">Some text in the Modal..</p> </div> </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda