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

385
Views
¿Por qué mi matriz no vuelve a 0 (javascript)?

 var a = ["red", "blue", "green"]; var i = 0 $(".button").click(function() { i++; if (i >= 3) { i = 0; } console.log(i); $("body").addClass(a[i]); console.log(a[i]); });
 .red { background-color: red; } .blue { background-color: blue; } .green { background-color: green; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" class="button">Click</button>

Puede cambiar de blanco a azul y verde, pero si vuelvo a hacer clic en el botón, no cambiará a rojo, azul y verde. Pero la consola muestra 1,2,0,1,2,0 y también el color azul, verde, rojo, azul.

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

0

Su problema es que nunca elimina la clase anterior, por lo que eventualmente el cuerpo tiene las tres clases y solo usa la última. Agregar

 $("body").removeClass(a[i]);

al comienzo de su controlador de clics:

 var a = ["red", "blue", "green"]; var i = 0 $(".button").click(function() { $("body").removeClass(a[i]); i++; if (i >= 3) { i = 0; } console.log(i); $("body").addClass(a[i]); console.log(a[i]); });
 .red { background-color: red; } .blue { background-color: blue; } .green { background-color: green; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <button type="button" class="button">Click</button> </body>

about 4 years ago · Juan Pablo Isaza Report

0

Este agregará y eliminará las diferentes clases de color hacia y desde el documento y no afectará a ninguna otra clase de documento que se haya asignado antes.

 const a = ["red", "blue", "green"]; ai=-1;a.col=function(){const cl=document.body.classList; cl.remove(this[this.i++]); cl.add(this[this.i%=3]); console.log(this.i,this[this.i]); }; document.querySelector(".button").onclick=ev=>a.col();
 .red { background-color: red; } .blue { background-color: blue; } .green { background-color: green; }
 <button type="button" class="button">Click</button>

about 4 years ago · Juan Pablo Isaza Report

0

Cambiar $("body").addClass(a[i]); a document.body.className = a[i];

Y no necesitas jQuery.

Al igual que:

 var a = ["red", "blue", "green"]; var i = 0 document.querySelector(".button").addEventListener( 'click', function() { i++; if (i >= 3) { i = 0; } console.log(i); document.body.className = a[i]; console.log(a[i]); });
 .red { background-color: red; } .blue { background-color: blue; } .green { background-color: green; }
 <button type="button" class="button">Click</button>

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!