jQuery 可点击行

发布于 2024-09-29 16:23:22 字数 939 浏览 8 评论 0原文

我有一个表,代表一个日历,可以展开和折叠表行。

  <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2024-10-06 16:23:22

即使您没有在点击处理程序中返回 false,页面也不应该滚动以响应点击。

页面是否足够短,当某些行折叠时,页面会变得足够短以使其全部适合视口? (因此浏览器自然会向上滚动以填充视口。)

更新如果是这种情况,您可能会考虑在调用期间尝试保留scrollTop

$(document).ready(function() {    
  $('tr.parent').click(function(){
    var scrollTop = document.body.scrollTop;        // <== Save the current value
    // *** Maybe append something to the page here to keep it tall***
    $(this).siblings('.child-' + this.id).toggle();
    // *** Maybe remove the appended thing now ***
    document.body.scrollTop = scrollTop;            // <== Restore it
    return false;
  });
});

如果所有这些位于 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:

$(document).ready(function() {    
  $('tr.parent').click(function(){
    var scrollTop = document.body.scrollTop;        // <== Save the current value
    // *** Maybe append something to the page here to keep it tall***
    $(this).siblings('.child-' + this.id).toggle();
    // *** Maybe remove the appended thing now ***
    document.body.scrollTop = scrollTop;            // <== Restore it
    return false;
  });
});

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.

垂暮老矣 2024-10-06 16:23:22

好吧,我尝试了 TJ 的建议来解决这个问题。

我将 varscrollTop = document.body.scrollTop 更改为 varscrollTop = window.pageYOffset 因为不知何故 document.body.scrollTop 总是返回 0(不不知道为什么)。 pageYOffset 为我返回正确的滚动位置。顺便说一句,这一切都是我在 Firefox 中完成的。

我最终得到了这段代码:

  <div id="bottomspacer" style="height: 1000px; display: none; "></div>
  <script type="text/javascript">
  $('tr.parent').click(function(){
    $('#bottomspacer').show();
    var scrollTop = window.pageYOffset;
    $(this).siblings('.child-' + this.id).toggle();
    document.body.scrollTop = scrollTop;
    $('#bottomspacer').hide();
    return false;
  });
  </script>

Ok so I tried TJ's suggestions to solve the problem.

I changed var scrollTop = document.body.scrollTop to var scrollTop = window.pageYOffset because somehow document.body.scrollTop always returns 0 (don't know why). the pageYOffset returns the correct scroll position for me. I did all this in Firefox by the way.

I ended up with this code:

  <div id="bottomspacer" style="height: 1000px; display: none; "></div>
  <script type="text/javascript">
  $('tr.parent').click(function(){
    $('#bottomspacer').show();
    var scrollTop = window.pageYOffset;
    $(this).siblings('.child-' + this.id).toggle();
    document.body.scrollTop = scrollTop;
    $('#bottomspacer').hide();
    return false;
  });
  </script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文