在这样的表中:
<table> <!-- Insert Row of bun here --> <tr id="meat"> <td>Hamburger</td> </tr> <!-- Insert Row of bun here --> </table> function AddBefore(rowId){} function AddAfter(rowId){}
解决方法
使用
function AddBefore(rowId){ var target = document.getElementById(rowId); var newElement = document.createElement('tr'); target.parentNode.insertBefore(newElement,target); return newElement; } function AddAfter(rowId){ var target = document.getElementById(rowId); var newElement = document.createElement('tr'); target.parentNode.insertBefore(newElement,target.nextSibling ); return newElement; }