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

163
Views
Evento de clic del mouse en una lista simple: mantenga el estilo activado y todos los demás se apaguen

Estoy usando una lista para filtrar una serie de otros elementos. Básicamente, necesito ayuda para que muestre visualmente cuál es el elemento seleccionado más recientemente,

  • Estado actual: mis eventos actuales de mouseover, mouseout y clic hacen posible que los eventos de pasar el mouse y hacer clic creen el formato de estilo deseado.
  • Efecto deseado 1: cuando el usuario hace clic en un primer elemento y saca el mouse, ese elemento debe permanecer formateado de cierta manera (actualmente se apaga debido al mouse out).
  • Efecto deseado 2: cuando el usuario hace clic en otro elemento posterior, ese elemento posterior debe tomar el formato y el primero debe perder ese formato.

HTML

 <div class="sidebar"> <div class="heading"> <h1>Select Country</h1> </div> <ul class="countryList"> <li class="countryItem" id="australia">Australia</li> <li class="countryItem" id="france">France</li> <li class="countryItem" id="germany">Germany</li> </ul> </div>

JS

 // I'm using this 'items' bit to control a lot of other code so would prefer a solution which keeps it. const items = document.getElementsByClassName('countryItem') console.log(Array.from(items)) // Some Mouse Events Array.from(items).forEach(function(item) { item.addEventListener('mouseover', function(handleMouseOver) { item.style.color = '#007dbc'; item.style.fontWeight = "700"; }) }) Array.from(items).forEach(function(item) { item.addEventListener('mouseout', function(handleMouseOut) { item.style.color = '#000'; item.style.fontWeight = "400"; }) }) Array.from(items).forEach(function(item) { item.addEventListener('click', function(handleClick) { item.style.color = '#0007dbc'; item.style.fontWeight = "700"; }) })

CSS

 ul { list-style-type: none; font-size:12px; margin: 0; padding: 20px; } li { cursor:pointer; }

¡Gracias!

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

0

La forma más fácil de resolver este problema es crear una variable de bandera para especificar qué elemento se resalta.

 const items = document.getElementsByClassName('countryItem') let clickedItem = '' Array.from(items).forEach(function(item) { item.addEventListener('mouseover', function(handleMouseOver) { item.style.color = '#007dbc'; item.style.fontWeight = "700"; }) }) Array.from(items).forEach(function(item) { item.addEventListener('mouseout', function(mouseOverEvent) { if (mouseOverEvent.target.id === clickedItem) return; item.style.color = '#000'; item.style.fontWeight = "400"; }) }) Array.from(items).forEach(function(item) { item.addEventListener('click', function(clicnEvent) { if (clickedItem) { const prevItem = document.getElementById(clickedItem) prevItem.style.color = '#000'; prevItem.style.fontWeight = "400"; } clickedItem = clicnEvent.target.id item.style.color = '#0007dbc'; item.style.fontWeight = "700"; }) })
 ul { list-style-type: none; font-size:12px; margin: 0; padding: 20px; } li { cursor:pointer; }
 <div class="sidebar"> <div class="heading"> <h1>Select Country</h1> </div> <ul class="countryList"> <li class="countryItem" id="australia">Australia</li> <li class="countryItem" id="france">France</li> <li class="countryItem" id="germany">Germany</li> </ul> </div>

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!