开发者

DOM - append in table

开发者 https://www.devze.com 2023-03-04 06:47 出处:网络
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));

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消