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

145
Views
El elemento de nodo se elimina solo la segunda vez por la función JS

El elemento de nodo se elimina solo la segunda vez, y los creados con la función de agregar se eliminan la primera vez.

 function move(o) { var parent = o.parentNode; parent.removeChild(o); parent.insertBefore(o, parent.firstChild); } function add() { var node = document.createElement('li'); var textnode = document.createTextNode("new"); node.appendChild(textnode); node.setAttribute("onclick", "move(this)"); document.getElementById("table").appendChild(node); id = "myList" } function del() { o = document.getElementById("table"); o.removeChild(o.firstChild); }
 <ol id='table'> <li onclick="move(this)">1</li> <li onclick="move(this)">2</li> <li onclick="move(this)">3</li> <li onclick="move(this)">4</li> <li onclick="move(this)">5</li> <li onclick="move(this)">6</li> </ol> <button onclick="add()">Append</button> <button onclick="del()">Del</button>

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

0

tienes que usar

 o.removeChild(o.firstElementChild);

en su función del().

Así que este es el código actualizado:

 <ol id="table"> <li onclick="move(this)">1</li> <li onclick="move(this)">2</li> <li onclick="move(this)">3</li> <li onclick="move(this)">4</li> <li onclick="move(this)">5</li> <li onclick="move(this)">6</li> </ol> <button onclick="add()">Append</button> <button onclick="del()">Del</button> <script> function move(o) { var parent = o.parentNode; parent.removeChild(o); parent.insertBefore(o, parent.firstChild); } function add() { var node = document.createElement("li"); var textnode = document.createTextNode("new"); node.appendChild(textnode); node.setAttribute("onclick", "move(this)"); document.getElementById("table").appendChild(node); id = "myList"; } function del() { o = document.getElementById("table"); o.removeChild(o.firstElementChild); } </script>
about 4 years ago · Juan Pablo Isaza Report

0

El firstChild no es un LI sino un nodo de texto

Usar

 o.removeChild(o.firstElementChild);

También delega el clic

 const table = document.getElementById("table") table.addEventListener("click",function(e) { const tgt = e.target; if (tgt.tagName==="LI") { move(tgt); } }) function move(o) { var parent = o.parentNode; parent.removeChild(o); parent.insertBefore(o, parent.firstElementChild); } function add() { var node = document.createElement('li'); var textnode = document.createTextNode("new"); node.appendChild(textnode); table.appendChild(node); } function del() { table.removeChild(table.firstElementChild); }
 <ol id='table'> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ol> <button onclick="add()">Append</button> <button onclick="del()">Del</button>

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!