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

150
Visualizações
Cree dinámicamente una página con JavaScript

Tengo 2 imágenes en HTML.

Me gustaría agregar dinámicamente un botón en JavaScript debajo de cada imagen que cuando se hace clic para ocultar la imagen y luego el siguiente clic para mostrar la imagen nuevamente.

La idea es utilizar:

  • querySelectorAll
  • en bucle
  • createElement (botón) a los botones
  • agregar agregarEventListener
  • añadir Niño

Código:

 <h2>Create a new site</h2> <p>Site – Manage Sites - New:</p> <dd><img class="screenshot" width="238" height="222" src="https://picsum.photos/200" alt="screenshot" /></dd> <p>“What would you like to call the website?”</p> <dd><img class="screenshot" width="238" height="222" src="https://picsum.photos/300" alt="screenshot" /></dd> img.screenshot { margin-bottom: 20px; margin-top: 20px; } .btn-styled { font-size: 14px; margin: 10px; padding: 0 5px; line-height: 2; } var eScreens = document.querySelectorAll('img.screenshot'); for (var i = 0; i <= eScreens.length; i++) { var button = document.createElement("button"); button.id = "myButton"; button.innerHTML = "Show"; button.className = "btn-styled"; button.style.background = "orange"; button.addEventListener("click", function() { }); var dd = document.getElementsByTagName("dd")[0]; dd.appendChild(button); }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Envolvería cada imagen en un elemento de figure para que cada una contenga una imagen, y cuando llegue a agregarla, un botón.

 // Cache the figures const figures = document.querySelectorAll('figure'); // For each add a button, and attach an event listener figures.forEach(figure => { addButton(figure); figure.addEventListener('click', handleClick, false); }); // Create a button, and add it to the end of // each figure element function addButton(figure) { const button = '<button>Hide</button>'; figure.insertAdjacentHTML('beforeend', button); } // Because the listener is attached to the figure // we need to check if the element we clicked was the // button. We can then get the image, and then // use a class to toggle its visibility // and update the text in the button function handleClick(e) { const { nodeName, parentNode, textContent } = e.target; if (nodeName === 'BUTTON') { const image = parentNode.querySelector('img'); image.classList.toggle('hide'); e.target.textContent = textContent === 'Hide' ? 'Show' : 'Hide'; } }
 figure { display: inline-block; margin: 0; } button { display: block; margin: 0 auto; } .hide { visibility: hidden; }
 <figure> <img src="https://dummyimage.com/100x100/ffaaaa/000" /> </figure> <figure> <img src="https://dummyimage.com/100x100/aaffaa/000" /> </figure> <figure> <img src="https://dummyimage.com/100x100/aaaaff/000" /> </figure>

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