如何获取/设置表格中的滚动位置?
我有一个具有固定标题和可滚动正文的表格。我想知道表中的滚动位置,以便我可以在回发时引用它并将其设置为回发前用户所在的位置。我更喜欢 JQuery 来做到这一点,但 Javascript 也可以。我不确定要在这里发布多少代码,因为我不确定问题是否是我不知道执行此操作的代码,或者我拥有的代码(css)干扰了我尝试过的解决方案。
有什么想法从哪里开始吗?有没有一种方法可以做到这一点?我尝试将其 (table style="overflow:auto; height:150" onclick="alert(this.scrollTop)") 放入表中,但它总是返回 0。
我也尝试了各种 JQuery 调用。他们都返回0。
I have a table that has a fixed header and a scrollable body. I want to know the scroll position in the table so that I can reference it on postback and set it to where the user was before postback. I would prefer JQuery to do this, but Javascript is fine too. Im not sure how much code to post here because Im not sure if the problem is that I just dont know the code to do this, or the code I have (css) is interfering with the solutions I have tried.
Any ideas where to start? Is there a method that does this? Ive tried putting this (table style="overflow:auto; height:150" onclick="alert(this.scrollTop)") in the table but it always returns 0.
Ive also tried various JQuery calls as well. They all return 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知(我每天都在处理这类事情),本机表无法按照您的描述处理滚动条。我认为您的表格可能实际上包含一个 div 或其他容器,它实际上正在滚动表格单元格。像这样的东西:
http://jsfiddle.net/mBDjx/46/
我建议您检查您的使用浏览器开发人员工具(通常是 F12)编写代码,并检查它是否只是一个表格,或者是另一个生成滚动的元素。
如果您意识到这是一个 div,只需检查 div 的 .scrollTop 属性,而不是在桌子上执行它:-)
希望它有效!
As far as I know (and I am dealing with this kind of stuff everyday), native tables can't handle scrollbars as your describing. I think that probably, your table actually contains a div or other container, which is actually scrolling the table cells. Something like this:
http://jsfiddle.net/mBDjx/46/
I recomend you to inspect your code using the browser developer tools (usually F12) and check if it is only a table, or another element the one which is generating the scroll.
If you realize that is is a div, just check the .scrollTop attribute of the div instead of doing it on the table :-)
Hope it works!
滚动的是 tbody,这就是您需要测试的内容。 this.scrollTop 指的是表格,表格顶部不改变位置。
It is the tbody that is scrolling and that is what you need to test. this.scrollTop is referring to the table , and the top of the table is not changing position.