• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

302
Vistas
Iterar elementos y alternar usando classList en javascript

Condiciones:

  1. Utilice la propiedad javascript y classList para invertir qué elementos tienen la clase .highlight .
  2. Básicamente iterar sobre todos los elementos <li> y alternar la clase de .highlight . en cada uno.
  3. No debe alterar nada en HTML y CSS.

¡Tu resultado debería ser así!

 /* app.js */ let list = document.querySelector('li'); list.classList.add('');
 /* index.css */ /*No need to touch anything in this file:*/ li { background-color: #B10DC9; } .highlight { background-color: #7FDBFF; }
 <!-- index.html --> <!DOCTYPE html> <head> <title>ClasList</title> </head> <body> <link rel="stylesheet" type="text/css" href="index.css"> <!--LEAVE THIS FILE ALONE!--> <ul> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> </ul> <script src="app.js"></script> </body> </html>

almost 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

La siguiente solución elimina el estilo de clase .highlight de <li> si el estilo de clase .highlight se aplica a <li> . Si el estilo de clase .highlight no se aplica al elemento <li> , el estilo de clase .highlight se agrega al elemento <li> .

Método 1

 /* The querySelectorAll() method is used to select all <li> elements. */ let list = document.querySelectorAll('li'); var listArray = [...list]; /* Each <li> element is checked using the forEach() method. */ listArray.forEach(element => { /* Is the ".highlight" class style applied to the <li> element? */ if(element.classList.contains("highlight")) { /* Remove the ".highlight" class style from the <li> element. */ element.classList.remove("highlight"); } else { /* Add the class style ".highlight" to the <li> element. */ element.classList.add("highlight"); } });
 li { background-color: #B10DC9; } .highlight { background-color: #7FDBFF; }
 <!DOCTYPE html> <head> <title>ClasList</title> <link rel="stylesheet" type="text/css" href="index.css"> </head> <body> <ul> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> </ul> <script src="app.js"></script> </body> </html>

Método-2

 /* The querySelectorAll() method is used to select all <li> elements. */ let list = document.querySelectorAll('li'); window.addEventListener('load', (event) => { for(let i = 0 ; i < list.length ; ++i) { if(list[i].classList.contains("highlight")) list[i].classList.remove("highlight"); else list[i].classList.add("highlight"); } });
 li { background-color: #B10DC9; } .highlight { background-color: #7FDBFF; }
 <!DOCTYPE html> <head> <title>ClasList</title> <link rel="stylesheet" type="text/css" href="index.css"> </head> <body> <ul> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> </ul> <script src="app.js"></script> </body> </html>

almost 3 years ago · Juan Pablo Isaza Denunciar

0

 /* The querySelectorAll() method is used to select all <li> elements. */ const li= document.querySelectorAll('li'); for(let i of li) { i.classList.toggle("highlight"); }
 li { background-color: #B10DC9; } .highlight { background-color: #7FDBFF; }
 <!DOCTYPE html> <head> <title>ClasList</title> <script src="node_modules/babel-polyfill/dist/polyfill.js" type="text/javascript"> </script> <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> </head> <body> <ul> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> <li>Hello</li> <li class="highlight">Hello</li> <li>Hello</li> </ul> </body> </html>

almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda