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

191
Views
Agregar hijo <img> a <a> en JavaScript

Aquí está mi código:

 let a = document.createElement('a'); a.role = "menuitem"; a.tabindex = "-1"; a.href = "#"; a.onclick = function() {reloadPageWithChanges()}; a.innerHTML = stories["Chapter1"]; let img = document.createElement('img'); img.src = `pictures/cars/car1.png`; img.style.marginRight = "3px"; img.style.marginLeft = "3px"; a.appendChild(img); body.appendChild(a);

Lo que hace este código es que crea un enlace que al onclick vuelve a cargar la página (con algunos pequeños cambios en la interfaz de usuario). Este enlace contiene el nombre ( stories["Chapter1"] ) y una imagen al lado. Sin embargo, esta imagen siempre se coloca en el lado derecho del texto. Quiero que se coloque en el lado izquierdo, de modo que todos estos enlaces (si hay más de ellos) tengan imágenes alineadas a la izquierda. ¿Hay alguna forma de hacerlo usando el DOM de JavaScript?

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

0

Puedes usar a.prepend(img); en lugar de a.appendChild(img);

 let a = document.createElement('a'); a.role = "menuitem"; a.tabindex = "-1"; a.href = "#"; a.onclick = function() { //reloadPageWithChanges() }; a.innerHTML = "myChapter"; //stories["Chapter1"]; let img = document.createElement('img'); img.src = `pictures/cars/car1.png`; img.style.marginRight = "3px"; img.style.marginLeft = "3px"; a.prepend(img); document.body.appendChild(a);

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!