开发者

save table structure to variable after 2nd row?

开发者 https://www.devze.com 2022-12-21 20:50 出处:网络
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

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

精彩评论

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

关注公众号