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

177
Views
¿Cómo asignar una imagen de fondo a html div usando javascript?

Alguien me puede explicar esto:

Tengo un div cuya imagen de fondo está funcionando:

 <section class="bottom-banner" id="imagebannerthree" style="background-image:url(https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg);"> </section>

pero cuando intenté cargar la imagen de fondo usando javascript, no se cargó ninguna imagen:

 document.getElementById('imagebannerthree').style.backgroundImage = checkImage("https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg"); function checkImage(url) { var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send(); if (http.status == 200) { return 'url(' + url + ')'; } else { document.getElementById('' + parentdiv).remove(); } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe usar comillas al asignar valor a style.backgroudImage

 document.getElementById('imagebannerthree').style.backgroundImage = "url('https://*****.com.au/wp-content/custom-products/1628/bottom-3.jpg')";

lo más probable es que obtenga el error "url no es una función".

about 4 years ago · Juan Pablo Isaza Report

0

 document.getElementById('imagebanner').style.backgroundImage = "url(https://piranhatc.com.au/wp-content/custom-products/1628/bottom-3.jpg)"; function checkImage(url) { var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send(); if (http.status == 200) { return 'url(' + url + ')'; } else { document.getElementById('' + parentdiv).remove(); } }
 .bottom-banner { object-fit: cover; width: 200px; height: 200px; }
 <section class="bottom-banner" id="imagebanner"> </section>

el nombre en id no coincide. Las funciones escritas a continuación parecen irrelevantes.

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!