jQuery 可点击行
我有一个表,代表一个日历,可以展开和折叠表行。
<tr class="parent" id="month1">
<th class="subheader">Januari</th>
<th></th><th></th>
</tr>
<tr class="row child-month1" id="day-1">
<td class="date"> 1 januari 2010</td>
<td>Bedrag </td>
<td>-817.0 </td>
</tr>
<tr class="row child-month1" id="day-2">
<td class="date"> 2 januari 2010</td>
<td>Bedrag </td>
<td> 0 </td>
</tr>
使用 jQuery,我使其可单击:
<script type="text/javascript">
$(document).ready(function() {
$('tr.parent').click(function(){
$(this).siblings('.child-' + this.id).toggle();
return false;
});
});
</script>
现在的问题是,单击表行后窗口始终滚动到顶部。我希望它保持在单击之前的滚动位置。
子行按预期折叠,但文档在单击后立即滚动到顶部,即使我在 .click 末尾返回 false... 我做错了什么?
I have a table, representing a calendar, that can expand and collapse table rows.
<tr class="parent" id="month1">
<th class="subheader">Januari</th>
<th></th><th></th>
</tr>
<tr class="row child-month1" id="day-1">
<td class="date"> 1 januari 2010</td>
<td>Bedrag </td>
<td>-817.0 </td>
</tr>
<tr class="row child-month1" id="day-2">
<td class="date"> 2 januari 2010</td>
<td>Bedrag </td>
<td> 0 </td>
</tr>
With jQuery I make it clickable:
<script type="text/javascript">
$(document).ready(function() {
$('tr.parent').click(function(){
$(this).siblings('.child-' + this.id).toggle();
return false;
});
});
</script>
The problem now, is that the window scrolls always to the top after a table row is clicked. I want it to stay at the scrolling position that it was before the click.
The child rows get collapsed as supposed to, but the document scrolls to the top immediately after the click, even though i have returned false at the end of .click...
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您没有在点击处理程序中返回 false,页面也不应该滚动以响应点击。
页面是否足够短,当某些行折叠时,页面会变得足够短以使其全部适合视口? (因此浏览器自然会向上滚动以填充视口。)
更新如果是这种情况,您可能会考虑在调用期间尝试保留
scrollTop
:如果所有这些位于
body
以外的容器中,您可能需要尝试将其保留在该容器中,但您明白了。这可能并不完美,具体取决于页面高度的变化程度,但它可能会有所帮助。Even if you didn't return false in the click handler, the page shouldn't be scrolling in response to the click.
Is it perhaps that the page is short enough that when some of the rows collapse, the page gets enough shorter that it all fits within the viewport? (And so naturally the browser scrolls up to fill the viewport.)
Update And if that's the case, you might consider trying to preserve
scrollTop
during the call:If all of this is in a container other than
body
, you may need to try to preserve it in that container instead, but you get the idea. This may not be perfect depending on how much the height of the page has changed, but it may help.好吧,我尝试了 TJ 的建议来解决这个问题。
我将
varscrollTop = document.body.scrollTop
更改为varscrollTop = window.pageYOffset
因为不知何故document.body.scrollTop
总是返回 0(不不知道为什么)。 pageYOffset 为我返回正确的滚动位置。顺便说一句,这一切都是我在 Firefox 中完成的。我最终得到了这段代码:
Ok so I tried TJ's suggestions to solve the problem.
I changed
var scrollTop = document.body.scrollTop
tovar scrollTop = window.pageYOffset
because somehowdocument.body.scrollTop
always returns 0 (don't know why). thepageYOffset
returns the correct scroll position for me. I did all this in Firefox by the way.I ended up with this code: