• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

197
Views
¿Cómo hacer que un elemento aún activo se mantenga activo incluso después de actualizar?

Estoy usando este código para activar el elemento actual. Encontré este código en "w3schools.com". en este código, si actualizo, el elemento actualmente activo desaparece. ¿Cómo se puede mantener activo el elemento que todavía está activo incluso después de la actualización?

 // Add active class to the current button (highlight it) var header = document.getElementById("myDIV"); var btns = header.getElementsByClassName("btn"); for (var i = 0; i < btns.length; i++) { btns[i].addEventListener("click", function() { var current = document.getElementsByClassName("active"); current[0].className = current[0].className.replace(" active", ""); this.className += " active"; }); }
 /* Style the buttons */ .btn { border: none; outline: none; padding: 10px 16px; background-color: #f1f1f1; cursor: pointer; font-size: 18px; } /* Style the active class, and buttons on mouse-over */ .active, .btn:hover { background-color: #666; color: white; }
 <h1>Active Button</h1> <p>Highlight the active/current (pressed) button.</p> <div id="myDIV"> <button class="btn">1</button> <button class="btn active">2</button> <button class="btn">3</button> <button class="btn">4</button> <button class="btn">5</button> </div>

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

0

Resolví tu problema, prueba esto

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> /* Style the buttons */ .btn { border: none; outline: none; padding: 10px 16px; background-color: #f1f1f1; cursor: pointer; font-size: 18px; } /* Style the active class, and buttons on mouse-over */ .active, .btn:hover { background-color: #666; color: white; } </style> </head> <body> <h1>Active Button</h1> <p>Highlight the active/current (pressed) button.</p> <div id="myDIV"> <button class="btn">1</button> <button class="btn active">2</button> <button class="btn">3</button> <button class="btn">4</button> <button class="btn">5</button> </div> <script> var header = document.getElementById("myDIV"); var btns = header.getElementsByClassName("btn"); for (var i = 0; i < btns.length; i++) { btns[i].addEventListener('click', function(e){ var current = document.getElementsByClassName("active"); current[0].classList.remove('active'); this.classList.add('active'); var array = []; array.push(btns); localStorage.setItem('element', this.innerText); }); } function setActivatedItem(){ var item = localStorage.getItem('element'); if(item){ for (var a = 0; a < btns.length; a++) { if(btns[a].innerText == item){ btns[a].classList.add('active'); }else{ btns[a].classList.remove('active'); } } } } window.onload == setActivatedItem(); </script> </body> </html>
about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error