How can I add rows <tr>
in to a table a any level(Top/Bottom/开发者_如何学GoIn between) using Javascript/jQuery
?
$(html).insertBefore($('table tr').eq(index));
Will insert some html
before the tr
with index specified.
A small demo
- to append to the bottom:
you have nothing to do, because you just have to find the parent element and then append it.
- to append to the top:
well, it is more complicated, but I think is possible. One way is following:
find the parent element from the rows and read all rows into an extra variable. Fortunatly the result will be an array of object. Then create a new row element and use the method unshift()
to put this at the begin of the array. Clean table's body and then add each element to body again.
http://api.jquery.com/prepend/
精彩评论