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

81
Views
Cómo escribir una función que devolverá colores al azar para un bloc de dibujo

Estoy creando un diseño de boceto de grabado simple y necesito crear una función que ingrese colores aleatorios al pasar el mouse sobre la cuadrícula. Hasta ahora he creado funciones con algunos colores específicos, pero no estoy seguro de adónde ir desde allí.

 const grid = document.querySelector('.grid'); const blackButton = document.querySelector('.blackbutton'); blackButton.addEventListener('click', blkButton); const clear = document.querySelector('.clearbutton') clear.addEventListener('click', clearGrid); const eraserButton = document.querySelector('.eraserbutton') eraserButton.addEventListener('click', ersButton) const rainbowButton = document.querySelector('.rainbowbutton') rainbowButton.addEventListener('click', rbwButton) function getGrid(gridNumber) { for (let i = 1; i <= gridNumber * gridNumber; i++) { const box = document.createElement('div'); box.classList.add('box'); grid.appendChild(box); } } getGrid(16); function blkButton() { grid.querySelectorAll(".box").forEach(box => box.addEventListener("mouseover", () => { box.style.background = "black"; })) } function ersButton(){ grid.querySelectorAll(".box").forEach(box => box.addEventListener("mouseover", () => { box.style.background = "lightgray" })) } function rbwButton(){ } function clearGrid() { const tgt = ".box" grid.querySelectorAll(".box").forEach(box => box.removeAttribute('style') ) }
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Hice algo muy similar en Angular que he mostrado aquí. Esto es para referencia. Modifique la sintaxis si es necesario.

 function blkButton() { grid.querySelectorAll(".box").forEach(box => box.addEventListener("mouseover", () => { box.style.background = this.getColor(); })) } rgb = []; function getColor(){ for (var i = 0; i < 3; i++) { this.rgb.push(Math.floor(Math.random() * 255)); } return 'rgb(' + this.rgb.join(',') + ')'; }
about 4 years ago · Santiago Gelvez 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!