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

138
Views
Establecer style.background como una var no funciona

Modifiqué este código Javascript en funcionamiento configurando style.background como var "x" y ya no cambia. ¿Dónde está el problema?

 <script> var myVar = setInterval(setColor, 300); function setColor() { /* This commented-out code works. var x = document.body; x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow"; */ var x = document.body.style.backgroundColor; // not working x = x == "yellow" ? "pink" : "yellow"; } </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Su valor de asignación a la variable "x", no document.body.style.backgroundColor , no lo almacene en una variable.

 <script> var myVar = setInterval(setColor, 300); function setColor() { var x = document.body.style.backgroundColor; document.body.style.backgroundColor = x == "yellow" ? "pink" : "yellow"; } </script>

about 4 years ago · Juan Pablo Isaza Report

0

Eso es porque en el código original:

 function setColor() { var x = document.body; x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow"; }

x es una referencia de document.body, sin embargo, en su código, cuando lo hace:

 x = x == "yellow" ? "pink" : "yellow";

x ya no es igual a una referencia de document.body, ahora es una cadena, ya sea rosa o amarilla. Entonces, x no cambiará el html porque es solo una variable de cadena.

Si desea que el código funcione, debe agregar este código detrás de la declaración x.

 document.body.style.backgroundColor = x
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!