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

268
Visualizações
¿Cómo agregar la etiqueta <br> en dom (vainilla javascript)? No puedo mostrar mi siguiente bucle en una nueva línea
const start = document.getElementById("start"); const end = document.getElementById("end"); const btnShowEven = document.getElementById("showEven"); const display = document.getElementById("display"); const br = document.createElement("br"); btnShowEven.addEventListener("click", myFunction); function myFunction() { for (let i = start.value; i <= end.value; i++) { if (i % 2 == 0) { console.log(`${i} is an even number.`); display.textContent += `${i} is an even number.`; display.createElement += br; } } }

cómo añadir
etiqueta en dom (vainilla javascript)? No puedo mostrar mi siguiente bucle en una nueva línea.

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="main.css" /> <title>Document</title> </head> <body> <input type="text" id="start" /> <input type="text" id="end" /> <button id="showEven">Show even numbers</button> <hr /> <p id="display"></p> <script src="main.js"></script> </body> </html>

Soy un principiante aquí. Lo siento. Aquí está mi archivo HTML. La salida es todas mis pantallas de bucle en 1 línea. Quería mostrar todos los números pares en una nueva línea.

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

0

Aquí hay una solución fácil. Simplemente use innerHTML en lugar de intentar crear nuevos elementos.

 const start = document.getElementById("start"); const end = document.getElementById("end"); const btnShowEven = document.getElementById("showEven"); const display = document.getElementById("display"); const br = document.createElement("br"); btnShowEven.addEventListener("click", myFunction); function myFunction() { for (let i = start.value; i <= end.value; i++) { if (i % 2 == 0) { console.log(`${i} is an even number.`); display.innerHTML += `${i} is an even number.<br>`; } } }
 <input value="1" id="start"> <input value="10" id="end"> <button id="showEven">Click</button> <div id="display"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Puede usar \n para simular una etiqueta <br> . ¿Cómo usarlo? así es cómo

 display.innerText += '${i} is an even number. \n';
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