Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
there are 5 "li" element ,how to exchange the first "li" element and the last "li" element?
<pre>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</pre>

there are 5 "li" element ,i want to exchange the first "li" element and the last "li" element? just like this:

<pre>
<ul>
<li>5</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>1</li>
</ul>
</pre>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Question is similar to this one: Changing the order of elements

Easier if you have unique ids, but it is possible with tag names.

(function() {
  let list = document.querySelectorAll("pre > ul > li")
  list[0].parentNode.insertBefore(list[4], list[0])
  list[0].parentNode.append(list[0]);
})();
<pre>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</pre>

about 4 years ago · Juan Pablo Isaza Denunciar

0

The naive idea is to get a list of li's, then you pop the first time, then push the same item onto the list, then replace all the children.

Alternatively, Get the content of the first Li, last Li, then replace the values. The first and the second options are semantically different. One copies nodes, while the other merely copies inner text.

about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda