为什么表tr高度返回0

时间:2020-01-15 03:45:18

标签: jquery html

将元素tr添加到表中之后。我想滚动到那个tr。因此,我需要获取tr的高度,但它始终返回0。请帮助我。

我的代码如下:

$('#desk_customerlister table tbody tr.selected').removeClass('selected');
$('#desk_customerlister table tbody').append("<tr class='selected'><th></th><td class='invisible-cell' id='IsDeleted'>false</td><td class='invisible-cell' id='IsEdited'>false</td><td class='invisible-cell' id='Id'>" + obj.Id + "</td><td id='Code'>" + row.Code + "</td><td id='Name'>" + row.Name + "</td><td id='Address'>" + row.Address + "</td><td id='Contact'>" + row.Contact + "</td><td id='Phone'>" + row.Phone + "</td><td id='Email'>" + row.Email + "</td><td class='invisible-cell' id='CreatedAt'>" + obj.ModifiedAt + "</td><td class='invisible-cell' id='CreatedBy'>" + row.UserName + "</td><td class='invisible-cell' id='ModifiedAt'>" + obj.ModifiedAt + "</td><td class='invisible-cell' id='ModifiedBy'>" + row.UserName + "</td></tr>");

var lastrow = $('#desk_customerlister table tbody tr.selected:first');

console.log('Height = ' + lastrow.height());

$('#desk_customerlister').scrollTop((lastrow.index() + 1) * lastrow.height() - $('#desk_customerlister').height() + $('#desk_customerlister thead').height());

1 个答案:

答案 0 :(得分:0)

我找到了HTML就绪后可以做的答案:

$('#desk_customerlister table tbody tr.selected').removeClass('selected');
$('#desk_customerlister table tbody').append("<tr class='selected'><th></th><td class='invisible-cell' id='IsDeleted'>false</td><td class='invisible-cell' id='IsEdited'>false</td><td class='invisible-cell' id='Id'>" + obj.Id + "</td><td id='Code'>" + row.Code + "</td><td id='Name'>" + row.Name + "</td><td id='Address'>" + row.Address + "</td><td id='Contact'>" + row.Contact + "</td><td id='Phone'>" + row.Phone + "</td><td id='Email'>" + row.Email + "</td><td class='invisible-cell' id='CreatedAt'>" + obj.ModifiedAt + "</td><td class='invisible-cell' id='CreatedBy'>" + row.UserName + "</td><td class='invisible-cell' id='ModifiedAt'>" + obj.ModifiedAt + "</td><td class='invisible-cell' id='ModifiedBy'>" + row.UserName + "</td></tr>");

var lastrow = $('#desk_customerlister table tbody tr.selected:first');

$(function() {
	console.log('Height = ' + lastrow.height());
	$('#desk_customerlister').scrollTop((lastrow.index() + 1) * lastrow.height() - $('#desk_customerlister').height() + $('#desk_customerlister thead').height());
});

相关问题