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

153
Views
Quiere alternar 2 listas diferentes al hacer clic en dos botones diferentes con el mismo código de script Java

Problemas
Hay los siguientes problemas que son los siguientes:

  • La primera lista funciona bien, pero tiene el problema de que las viñetas desaparecen al hacer clic en el botón. Sé que se puede resolver usando .Card1.short>li:nth-child(n + 6){display: block;} solo, pero necesito para ocultar la etiqueta de listas por defecto
  • El segundo problema es que el segundo botón no funciona en su propia lista, sino que cambia el primero : necesito usar el mismo JS único , ya que puede haber más de 10 listas . También el innerHTML de ambos botones funciona bien ya que cambia individualmente al hacer clic

Código

 function reed(saad) { var ul = document.getElementById("Card1"); button = document.getElementById("demo1"); ul.classList.toggle("short"); if (saad.innerHTML == "Show More") { saad.innerHTML = "Show Less"; } else { saad.innerHTML = "Show More"; } }
 .Card1.short > li:nth-child(n + 6) { display: block; } .Card1 > li:nth-child(n + 6) { display: none; } ul{float:left;}
 <ul class="Card1" id="Card1"> <li>Windows 10</li> <li>8GB Ram</li> <li>1TB SSD</li> <li>Intel Core i7 (11th Gen)</li> <li>NVIDIA GeForce</li> <li>Full HD Display</li> <li>Dolby Audio</li> <li>1 Warranty</li> <button id="demo1" onclick="reed(this)">Show More</button> </ul> <ul class="Card2" id="Card2"> <li>Windows 20</li> <li>16GB Ram</li> <li>10TB SSD</li> <li>Intel Core i15 (21th Gen)</li> <li>NVIDIA</li> <li>Double HD Display</li> <li>Dolby Audio</li> <li>2 Warranty</li> <button id="demo2" onclick="reed(this)">Show More</button> </ul>

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

0

He editado tu código. En lugar de seleccionar un elemento por id , use this para obtener parentElement y alternar la clase short .

Además, si desea que la lista esté marcada con una viñeta, use display:list-item . La palabra clave list-item hace que el elemento genere un cuadro de pseudoelemento ::marker.

 function reed(saad) { saad.parentElement.classList.toggle("short"); if (saad.innerHTML == "Show More") { saad.innerHTML = "Show Less"; } else { saad.innerHTML = "Show More"; } }
 .Card1.short > li:nth-child(n + 6), .Card2.short > li:nth-child(n + 6) { display: list-item; } .Card1 > li:nth-child(n + 6), .Card2 > li:nth-child(n + 6) { display: none; } ul{ float:left; }
 <ul class="Card1" id="Card1"> <li>Windows 10</li> <li>8GB Ram</li> <li>1TB SSD</li> <li>Intel Core i7 (11th Gen)</li> <li>NVIDIA GeForce</li> <li>Full HD Display</li> <li>Dolby Audio</li> <li>1 Warranty</li> <button id="demo1" onclick="reed(this)">Show More</button> </ul> <ul class="Card2" id="Card2"> <li>Windows 20</li> <li>16GB Ram</li> <li>10TB SSD</li> <li>Intel Core i15 (21th Gen)</li> <li>NVIDIA</li> <li>Double HD Display</li> <li>Dolby Audio</li> <li>2 Warranty</li> <button id="demo2" onclick="reed(this)">Show More</button> </ul>

about 4 years ago · Juan Pablo Isaza Report

0

Puede probar lo siguiente, que apunta al elemento principal del botón, en lugar de usar una identificación fija. También en el css, "list-item" en lugar de "block" mantendrá las viñetas.

 function reed(saad) { saad.parentElement.classList.toggle("short"); if (saad.innerHTML == "Show More") { saad.innerHTML = "Show Less"; } else { saad.innerHTML = "Show More"; } }
 <ul class="Card" id="Card1"> <li>Windows 10</li> <li>8GB Ram</li> <li>1TB SSD</li> <li>Intel Core i7 (11th Gen)</li> <li>NVIDIA GeForce</li> <li>Full HD Display</li> <li>Dolby Audio</li> <li>1 Warranty</li> <button id="demo1" onclick="reed(this)">Show More</button> </ul> <ul class="Card" id="Card2"> <li>Windows 20</li> <li>16GB Ram</li> <li>10TB SSD</li> <li>Intel Core i15 (21th Gen)</li> <li>NVIDIA</li> <li>Double HD Display</li> <li>Dolby Audio</li> <li>2 Warranty</li> <button id="demo2" onclick="reed(this)">Show More</button> </ul>
 .Card.short > li:nth-child(n + 6) { display: list-item; } .Card > li:nth-child(n + 6) { display: none; } ul{float:left;}

Marque este violín si desea generar las tarjetas usando javascript: https://jsfiddle.net/anixiapetha/o7fyjcnL/2/

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!