Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
Los divs creados dinámicamente con clase agregada no tienen estilo

Estoy tratando de generar algunos divs dinámicamente a través de un bucle for. No tienen el estilo de color y tamaño, aunque he agregado una clase y puedo ver la clase en las herramientas de desarrollo.

 public addDivs() { for(let i = 0; i < 10; i++) { let div = document.createElement('div'); div.classList.add('test'); div.innerHTML = 'testing' } } .test { color:red; background-color:green; height: 40px; width: 40px; }

Cuando agrego un estilo como este, funciona.

 public addDivs() { for(let i = 0; i < 10; i++) { let div = document.createElement('div'); div.innerHTML = 'testing' div.style.color = 'red'; } }

¿Es posible (sin JQuery) obtener el estilo de un archivo css?

editar: también debo agregar que comenzó a fallar solo cuando puse el código en un bucle for.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Agregue el elemento después de crearlo. Las clases y estilos existentes funcionan con nodos creados dinámicamente.

 function addDivs() { for(let i = 0; i < 10; i++) { let div = document.createElement('div'); div.classList.add('test'); div.innerHTML = 'testing:'+i; document.querySelector("body").appendChild(div); } } addDivs();
 .test { color:red; background-color:green; height: 40px; width: 100px; margin-bottom:4px; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!