How to insert row at end of table with HTMLTableElement.insertRow

后端 未结 4 1825
天命终不由人
天命终不由人 2020-12-05 19:07

Background and Setup

I\'m trying to create a table from tabular data in javascript, but when I try inserting rows into a table element, it inserts in the opposite

4条回答
  •  不思量自难忘°
    2020-12-05 19:18

    The .insertRow and .insertCell methods take an index. In absence of the index, the default value is -1 in most browsers, however some browsers may use 0 or may throw an error, so it's best to provide this explicitly.

    To to an append instead, just provide -1, and an append will be done

    var row = table.insertRow(-1);
    var cell = row.insertCell(-1);
    

    https://developer.mozilla.org/en-US/docs/DOM/table.insertRow

提交回复
热议问题