简体   繁体   English

onclick事件返回false后,anchor标签滚动到顶部不起作用

[英]anchor tag scrolls to top after onclick event return false doesn't work

I have a click event that will not return false after I have called my ajax function i was wondering how I can get around this. 我有一个单击事件,在调用ajax函数后,该事件不会返回false,我想知道如何解决这个问题。 I have used e.preventDefault() which works so that the url doesn't add a hash but when I use return false after my ajax function it doens't work but If I do it the other way round it works but cancels out my ajax call. 我已经使用过e.preventDefault(),它可以正常工作,以便该URL不添加哈希,但是当我在ajax函数之后使用return false时,它不起作用,但是如果我反其道而行之,则可以取消我的阿贾克斯电话。

<a href="#">Click me</a>

$('a').click(function(e) {
    getProducts(..., ...., ....) //my ajax function
    e.preventDefault() // doesn't work
    return false; doesn't work either
});

cancel the default action using preventDefault function. 使用preventDefault函数取消默认操作。

$('a').click(function(e) { 
    getProducts(..., ...., ....) //my ajax function
    e.preventDefault(); 
}); 

a few thoughts here. 这里有一些想法。 #1 e.preventDefault() only works if you pass in the event via function(e) {...} you aren't which would be a problem. #1 e.preventDefault()仅在您通过function(e){...}传递事件时才有效,但这不是问题。 #2 From what you've described the only thing I can suggest is that you've got a javascript error which leads to thought #3...If you want more help you're going to have to give us more to go on #2根据您的描述,我唯一能建议的就是您遇到了一个JavaScript错误,该错误会导致思想#3 ...如果您需要更多帮助,则必须给我们更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM