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

230
Views
cambiar el color de fondo del botón en el que se hizo clic en reaccionar: js

tengo algunos botones cada vez que hago clic en un botón quiero que se resalte cambiando el color de fondo del botón hasta que se haga clic en cualquier otro botón

 function clickedtime(e) { var time = e.target.value; settime(time); }
 <TabPanel className="tabcss" style={styles.tabpannel}> <button onClick={clickedtime} value="9:00am" style={styles.button} > 9:00am </button> <button onClick={clickedtime} value="9:30am" style={styles.button} > 9:30am </button> .... </TabPanel>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use el controlador de eventos y useState para configurar el color al hacer clic así

 const[btn,setBtn]=useState([0,0]);//mean two button const[sel,setSel]=useState(0);//mean 0 button is selected by default function clickedtime(e,index) { var time = e.target.value; settime(time); setSel(index)//selected btn is 0in this case }

ahora, para su panel de pestañas, cambie el estilo a estilo = {{backgroundColor: col}} también cambie el estilo del panel de pestañas como mencioné

 <TabPanel className="tabcss" style={styles.tabpannel}> btn.map((item,index)=>{ sel==index? <button onClick={(index)=>{clickedtime(index)}} value="9:00am" style={{backgroundColor:"red"}} > 9:00am </button> : <button onClick={clickedtime} value="9:00am" style={{backgroundColor:"white"}} > 9:00am </button> } .... </TabPanel>
about 4 years ago · Juan Pablo Isaza Report

0

Puede agregar todos los botones a una clase, agregarles un detector de eventos y luego tener otra clase llamada "hacer clic" adjunta a ellos cuando se activa el evento de clic.

 var buttons = document.getElementsByClassName("time-button"); var click_listener = function(event) { //console.log("clicked " + event.target); for (var i = 0; i < buttons.length; i++) { buttons[i].classList.remove("clicked"); } event.target.classList.add("clicked"); }; for (var i = 0; i < buttons.length; i++) { buttons[i].addEventListener("click", click_listener, false); }
 .clicked { background-color: orange; }
 <button class="time-button">9:30 am</button> <button class="time-button">9:00 am</button> <button class="time-button">8:30 am</button> <button class="time-button">8:00 am</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!