本文介绍了如何以编程方式将表格滚动到特定的tr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过使用JavaScript或jQuery将html表格滚动到特定的tr。目前我可以获得所选tr的偏移量。而且我使用scrollTop方法。我尝试了以下方法,但它不适用于我:
var table = document.getElementById(table);
var tr = table.getElementsByTagName(tr)[3];
var scrollTo = tr.offsetTop;
table.scrollTop = scrollTo;
我也试过jquery:
<$ p 。$ p> $( '#表')动画({scrollTop的:0},50);
任何人都可以帮我解决问题吗?
解决方案
试试这个:
var w = $(window);
var row = $('#tableid')。find('tr')。eq(line);
if(row.length){
w.scrollTop(row.offset()。top - (w.height()/ 2));
}
参考:
I want to scroll the html table to particular tr by using Javascript or jquery. Currently I can get the offset of the selected tr .And I am using the scrollTop method .I have tried the following but it is not working for me :
var table = document.getElementById("table");
var tr = table.getElementsByTagName("tr")[3];
var scrollTo = tr.offsetTop;
table.scrollTop = scrollTo;
also I tried with jquery :
$('#table').animate({scrollTop:0},50);
can anybody help me where am I getting wrong ?
解决方案
try this : http://jsfiddle.net/SZKJh/
var w = $(window);
var row = $('#tableid').find('tr').eq( line );
if (row.length){
w.scrollTop( row.offset().top - (w.height()/2) );
}
reference :
https://stackoverflow.com/a/7853216/1982680
这篇关于如何以编程方式将表格滚动到特定的tr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!