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

119
Views
Agregar resultado de matriz a una cadena

Este es un pequeño script que aleatoriza qué imagen debe mostrar. Puedo establecer fácilmente esto en un <img> usando document.getElementById("pic").src = randPics[randNumGen] , pero la etiqueta de background requiere que use url() . ¿Cómo puedo establecer document.body.style.background en "url (y la imagen aleatoria aquí)"? Por favor, no hay bibliotecas JS.

 const randPics = ["https://i.ibb.co/6JBbMYm/pic1.jpg", "https://i.ibb.co/rHjTdfc/pic2.jpg"]; window.onload = choosePicture(); function choosePicture() { let randNumGen = Math.floor(Math.random() * randPics.length); console.log(randNumGen); document.body.style.background = "randPics[randNumGen]"; }
 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles.css"> <script async src="index.js" type="text/javascript"></script> </head> <body> <img width="500px" src="#" alt="pic" id="pic"> </body> </html>

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

0

var url = randPics[randNumGen]; document.body.style.backgroundImage = "url('"+ url + "')";
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!