简体   繁体   中英

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. 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.

<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.

$('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. #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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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