Delete all rows in an HTML table

后端 未结 19 1755
既然无缘
既然无缘 2020-12-04 12:03

How can I delete all rows of an HTML table except the \'s using Javascript, and without looping through all the rows in the table? I have a very huge

相关标签:
19条回答
  • 2020-12-04 12:24

    const table = document.querySelector('table'); table.innerHTML === ' ' ? null : table.innerHTML = ' '; the above javascript worked fine for me. It checks to see if the table contains any data and then clears everything including the header.

    0 讨论(0)
  • 2020-12-04 12:26

    If you do not want to remove th and just want to remove the rows inside, this is working perfectly.

    var tb = document.getElementById('tableId');
      while(tb.rows.length > 1) {
      tb.deleteRow(1);
    }
    
    0 讨论(0)
  • 2020-12-04 12:29

    Just Clear the table body.

    $("#tblbody").html("");
    
    0 讨论(0)
  • 2020-12-04 12:31

    Assuming you have just one table so you can reference it with just the type. If you don't want to delete the headers:

    $("tbody").children().remove()
    

    otherwise:

    $("table").children().remove()
    

    hope it helps!

    0 讨论(0)
  • 2020-12-04 12:34

    this will remove all the rows:

    $("#table_of_items tr").remove(); 
    
    0 讨论(0)
  • 2020-12-04 12:35

    this would work iteration deletetion in HTML table in native

    document.querySelectorAll("table tbody tr").forEach(function(e){e.remove()})
    
    0 讨论(0)
提交回复
热议问题