Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
mostrar la lista original después de la función de reproducción aleatoria

Ver mi código cortado. Tengo una lista html y una función window.onload = shuffleNodes; que muestran automáticamente una lista aleatoria. ¿Cómo agregar una función de botón para mostrar el orden de la lista original? ¡¡¡Muchísimas gracias!!!

 var list = document.getElementById("something"), button = document.getElementById("shuffle"); function shuffle(items) { var cached = items.slice(0), temp, i = cached.length, rand; while(--i) { rand = Math.floor(i * Math.random()); temp = cached[rand]; cached[rand] = cached[i]; cached[i] = temp; } return cached; } function shuffleNodes() { var nodes = list.children, i = 0; nodes = Array.prototype.slice.call(nodes); nodes = shuffle(nodes); while(i < nodes.length) { list.appendChild(nodes[i]); ++i; } } window.onload = shuffleNodes;
 <dl id="something"> <dd>one</dd> <dd>two</dd> <dd>three</dd> <dd>four</dd> <dd>five</dd> <dd>six</dd> <dd>seven</dd> <dd>eight</dd> <dd>nine</dd> <dd>ten</dd> </dl> <hr> <button onclick="myFunction()">ORDINATE LIST</button>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Sugeriría almacenar una lista de los nodos en una matriz de nodos orderedNodes y luego barajar antes de mostrar inicialmente.

Luego podemos volver a mostrar fácilmente la lista ordenada, o barajarla nuevamente, usando cualquiera de los dos botones.

 const orderedNodes = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; function shuffle(items) { var cached = items.slice(0), temp, i = cached.length, rand; while(--i) { rand = Math.floor(i * Math.random()); temp = cached[rand]; cached[rand] = cached[i]; cached[i] = temp; } return cached; } function displayShuffled() { displayNodes(shuffle(orderedNodes)); } function displayOrdered() { displayNodes(orderedNodes) } function displayNodes(nodes) { clearNodes(); nodes.forEach(displayNode); } function clearNodes() { const list = document.getElementById("something"); list.innerHTML = ''; } function displayNode(nodeText) { const list = document.getElementById("something"); const el = document.createElement("dd"); el.appendChild(document.createTextNode(nodeText)); list.appendChild(el); } window.onload = displayShuffled;
 <dl id="something"> </dl> <hr> <button onclick="displayOrdered()">Order List</button> <button onclick="displayShuffled()">Shuffle List</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Un pequeño truco sería almacenar el HTML innerHTML del elemento antes de barajar y volver a ese HTML al hacer clic en el botón.

 var list = document.getElementById("something"), button = document.getElementById("shuffle"); let original = list.innerHTML; function shuffle(items) { var cached = items.slice(0), temp, i = cached.length, rand; while (--i) { rand = Math.floor(i * Math.random()); temp = cached[rand]; cached[rand] = cached[i]; cached[i] = temp; } return cached; } function myFunction(){ list.innerHTML = original; } function shuffleNodes() { var nodes = list.children, i = 0; nodes = Array.prototype.slice.call(nodes); nodes = shuffle(nodes); while (i < nodes.length){ list.appendChild(nodes[i]); ++i; } } window.onload = shuffleNodes;
 <dl id="something"> <dd>one</dd> <dd>two</dd> <dd>three</dd> <dd>four</dd> <dd>five</dd> <dd>six</dd> <dd>seven</dd> <dd>eight</dd> <dd>nine</dd> <dd>ten</dd> </dl> <hr> <button onclick="myFunction()">ORDINATE LIST</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda