开发者

save table structure to variable after 2nd row?

if in html table have 5 row, 1 and 2 are safe, 3,4,5 must be saved as html structure to a variable. like this

var after2nd开发者_运维知识库Content= '<tr><td>3</td></tr><tr><td>4</td></tr><tr><td>5</td></tr>' 


Since this is a continuation of your previous post, you can use this

var elemRemoved = $("#tbl1 tr:gt(1)").detach();
var removedContents = $("<div />").append(elemRem).remove().html();
alert ( removedContents );


As jQuery does not have an outerHTML function, it's a bit tricky:

$('<table>').append($('tr:gt(1)').clone()).remove().html();

That is:

  • Create a dummy table element
  • Append the rows after the second row (gt() is zero-indexed)
  • Get the contents of this table (including the <tr> tags) and remove the element

If you simply used $('tr:gr(1)').html(), you'd probably just get the contents of the first <tr>.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜