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

226
Views
Cómo hacer que la posición sea absoluta en lugar de 'indefinida' después de usar setAttribute

Estoy intentando asignar propiedades de una matriz llamada 'hechizo' a elementos img recién creados alojados dentro de un contenedor div.

 for (let i = 0; i <spell.length; i++){ let styles = spell[i] let container = document.getElementById('imagecontainer'); let newImg = document.createElement('img'); let top = styles.top; let left = styles.left; let imgSrc = styles.src; newImg.setAttribute('width','25px'); newImg.setAttribute('left',left); newImg.setAttribute('top',top); newImg.setAttribute('src',imgSrc); newImg.setAttribute('height','25px') newImg.setAttribute('position','absolute'); console.log(newImg.position) container.appendChild(newImg); }

sin embargo, cuando ejecuto el código, registra newImg.position como indefinido para todas las imágenes. no estoy seguro de lo que está mal

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

0

Establezca las propiedades del atributo de estilo: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style

 const spell = [{ top: "10px", left: "10px", src: "foo.png" }, { top: "50px", left: "50px", src: "bar.png" }]; const container = document.getElementById('imagecontainer'); for (let i = 0; i < spell.length; i++) { let styles = spell[i]; let newImg = document.createElement('img'); let top = styles.top; let left = styles.left; let imgSrc = styles.src; newImg.style.width = '25px'; newImg.style.height = '25px'; newImg.style.top = top; newImg.style.left = left; newImg.style.position = 'absolute'; newImg.setAttribute("src", imgSrc); console.log(newImg.style.position); container.appendChild(newImg); }
 <div id="imagecontainer"></div>

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!