滚动条卡在 jQuery show & 中隐藏
我使用 jQuery 来显示和隐藏各种 div 以对内容进行分页。然而,在选择显示或隐藏 div 的选项(例如,第 1、2 或 3 页)后,我会立即不规则地上下滚动,而不是让我向下滚动页面。我根本不知道是什么原因造成的,并且在 Stack Overflow 和 Google 上进行搜索后,这似乎不是其他人遇到过的问题。有谁对可能导致此问题的原因以及如何纠正它有建议吗?
I'm using jQuery to show and hide various divs to paginate content. However, immediately after selecting an option that shows or hides the divs (for example, page 1, 2 or 3) and scrolls erratically up and down rather than let me scroll down the page. I have no idea at all what could be doing this, and having searched Stack Overflow and Google it doesn't appear to be a problem that anyone else has faced. Does anyone have advice on what could be causing this problem and how to rectify it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的意思是不希望页面跳转到顶部?如果是这样,则发生这种情况是因为您的
标记具有
href="#"
。这是浏览器的正常行为,您可以在任何网站上进行测试。只需向下滚动页面,在 URL 末尾添加哈希值,然后按 Enter,浏览器就会移动到页面顶部。您正在使用 jQuery 将函数绑定到
标记上的单击事件,该标记将首先执行该函数,然后继续遵循
href
。为了防止这种继续,您可以从函数中返回 false; 或使用内置的 jQuery 函数preventDefault()
和stopPropagation()
以防止默认行为并停止事件传播 - 请参阅event.preventDefault() 与 return false因此,将代码更改为:
并对
a.page2 执行相同的操作代码>和
a.page3
绑定函数,页面将不再跳转。Do you mean that you do not want the page to jump to the top? If so, it is happening because your
<a>
tags havehref="#"
. This is normal behaviour for browsers and you can test this on any site. Just scroll down the page, add a hash to the end of the URL press Enter and the browser will move to the top of the page.You are using jQuery to bind a function to the click event on the
<a>
tags which will first execute the function and then continue to follow thehref
. To prevent this continuation you can eitherreturn false;
from the function or use in-built jQuery functionspreventDefault()
andstopPropagation()
to prevent the default behaviour and stop the event propagating - see event.preventDefault() vs. return falseSo change your code to:
And also do the same for the
a.page2
anda.page3
bound functions and the page will no longer jump.试试这个:
Try this :