jquery点击锚元素强制滚动到顶部?
jQuery 超链接 - href 值? text][1]
我遇到了问题使用 jquery 和附加到锚元素的单击事件。 [1]: jQuery 超链接 - href 值? “这个”所以问题似乎是重复的,并且接受的答案似乎并没有解决问题。 抱歉,如果这是不好的礼仪。
在我的 .ready() 函数中,我有:
jQuery("#id_of_anchor").click(function(event) { //start function when any update link is clicked
Function_that_does_ajax();
});
我的锚点看起来像这样:
<a href="#" id="id_of_anchor"> link text </a>
但是当单击链接时,ajax 函数会根据需要执行,但浏览器会滚动到页面顶部。 不好。
我尝试添加:
event.preventDefault();
在调用执行 ajax 的函数之前,但这没有帮助。 我缺少什么?
澄清
每种组合
return false;
event.preventDefault();
event.stopPropagation();
我已经使用了调用 js ajax 函数之前和之后的 。 它仍然滚动到顶部。
jQuery hyperlinks - href value? text][1]
I am running in to a problem using jquery and a click event attached to an anchor element.
[1]: jQuery hyperlinks - href value? "This" SO question seems to be a duplicate, and the accepted answer doesn't seem to solve the problem. Sorry if this is bad SO etiquette.
In my .ready() function I have:
jQuery("#id_of_anchor").click(function(event) { //start function when any update link is clicked
Function_that_does_ajax();
});
and my anchor looks like this:
<a href="#" id="id_of_anchor"> link text </a>
but when the link is clicked, the ajax function is performed as desired, but the browser scrolls to the top of the page. not good.
I've tried adding:
event.preventDefault();
before calling my function that does the ajax, but that doesn't help.
What am I missing?
Clarification
I've used every combination of
return false;
event.preventDefault();
event.stopPropagation();
before and after my call to my js ajax function. It still scrolls to the top.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这应该可行,你能澄清一下“之前”的意思吗? 你在做这个吗?
因为它应该可以工作,如果它不起作用,那么你就做错了什么,我们需要查看更多代码。 但是,为了完整起见,您也可以尝试以下操作:
编辑
这是此工作的示例。
这两个链接使用此代码:
正如您所看到的,它们工作得很好。
That should work, can you clarify what you mean by "before"? Are you doing this?
Because that should work in the sense that if it's not working YOU are doing something wrong and we'd need to see more code. However, for the sake of completeness, you could also try this:
EDIT
Here is an example of this working.
The two links use this code:
And as you can see they are working just fine.
你可以使用 javascript:void(0); 在 href 属性中。
you can use javascript:void(0); in href property.
正如 Tilal 上面所说:
保持锚标记看起来正确,允许您使用 JavaScript 为其分配点击处理程序,并且不滚动。 正是出于这个原因,我们广泛使用它。
As Tilal said above:
keeps the anchor tag looking right, allows you to assign click handlers to it with javascript, and doesn't scroll. We use it extensively just for this reason.
这个问题更多的是一个调试练习,因为它声明您正在使用 jQuery 事件中的正确函数对象。
在 jQuery 事件处理程序中,您可以执行以下两项基本操作中的一项或两项:
1.
防止元素的默认行为(A HREF =“#”,在您的情况下):
2。
阻止事件传播到周围 HTML 元素上的事件处理程序:
由于您没有获得预期的行为,因此您应该暂时删除 ajax 调用,将其替换为一些无害的代码,例如设置表单值,或者 -shudder- < em>alert("OK") 并测试您是否仍然滚动到顶部。
This question is more of a debugging exercise since it's stated that you are using the correct functions from the jQuery Event object.
In a jQuery event handler, you can do either, or both, of two basic things:
1.
Prevent the default behavior of the element (The A HREF="#", in your case):
2.
Stop the event from propagating to event handlers on the surrounding HTML elements:
Since you're not getting the expected behavior, you should take out the ajax call temporarily, replace it with some innocuous code like setting a form value, or -shudder- alert("OK") and test if you still scroll to the top.
只是丢失了
html 标签...这就是导致网页“跳起来”的原因。
并记住添加
到你的 a 的 css 中,这样它看起来就像鼠标悬停时的链接。
祝你好运。
just lose the
In the a html tag... that's what causing the web page to "jump up".
and remember to add
to your a's css so it will look like a link on mouseover.
Good luck.
我也遇到了同样的问题。
我认为当您在将元素附加到 DOM 之前绑定事件时会发生这种情况。
I've been having the same problem.
I think it occurs when you bind the event before appending the element to the DOM.
下面的代码对我帮助很大。 谢谢你!
It helped me a lot with the below code. Thank you!