jQuery的点击锚元素力量滚动到顶部? [英] jquery click on anchor element forces scroll to top?

查看:142
本文介绍了jQuery的点击锚元素力量滚动到顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://stackoverflow.com/questions/720970/jquery-hyperlinks-href-value">http://stackoverflow.com/questions/720970/jquery-hyperlinks-href-value[link文本] [1]

http://stackoverflow.com/questions/720970/jquery-hyperlinks-href-value[link text][1]

我使用jQuery和连接到一个锚元素的点击事件运行的一个问题。   [1]:<一href="http://stackoverflow.com/questions/720970/jquery-hyperlinks-href-value">http://stackoverflow.com/questions/720970/jquery-hyperlinks-href-value 这个,所以问题可能是重复的,而公认的答案似乎并没有解决问题。很抱歉,如果这是个坏SO礼仪。

I am running in to a problem using jquery and a click event attached to an anchor element. [1]: http://stackoverflow.com/questions/720970/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();
		});

和我的锚是这样的:

<a href="#" id="id_of_anchor"> link text </a>

但链接被点击时,所述AJAX功能时根据需要,但浏览器滚动到的页面的顶部。不好。

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.

我试着加入:

event.preventDefault();

打电话给我的函数,它的阿贾克斯之前,但这并不能帮助。 我在想什么?

before calling my function that does the ajax, but that doesn't help. What am I missing?

澄清

我用的每个组合

return false;
event.preventDefault(); 
event.stopPropagation();

在和我的电话给我的js AJAX功能之后。它仍然滚动顶端。

before and after my call to my js ajax function. It still scrolls to the top.

推荐答案

这应该工作,你能澄清你所说的前是什么意思?你在做这个?

That should work, can you clarify what you mean by "before"? Are you doing this?

jQuery("#id_of_anchor").click(function(event) {
    event.preventDefault();
    Function_that_does_ajax();
});

由于该工作在这个意义上,如果它不工作,你正在做的事情的错误,我们需要看到更多的code。然而,为了完整起见,你也可以试试这个:

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:

jQuery("#id_of_anchor").click(function() {
    Function_that_does_ajax();
    return false;
});

修改

下面是本工作的的一个例子。

这两个环节使用code:

The two links use this code:

$('#test').click(function(event) {
    event.preventDefault();
    $(this).html('and I didnt scroll to the top!');
});

$('#test2').click(function(event) {
    $(this).html('and I didnt scroll to the top!');
    return false;
});

正如你可以看到他们的工作就好了。

And as you can see they are working just fine.

这篇关于jQuery的点击锚元素力量滚动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆