jquery click on anchor element forces scroll to top?

两盒软妹~` 提交于 2019-11-30 01:53:59

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();
});

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;
});

EDIT

Here is an example of this working.

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.

you can use javascript:void(0); in href property.

As Tilal said above:

<a id="id_of_anchor" href="javascript:void(0)" />

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.

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):

jQuery("#id_of_anchor").click(function(evt) { 
  // ...
  evt.preventDefault(); // assuming your handler has "evt" as the first parameter
  return false;
}

2. Stop the event from propagating to event handlers on the surrounding HTML elements:

jQuery("#id_of_anchor").click(function(evt) { 
  // ...
  evt.stopPropagation(); // assuming your handler has "evt" as the first parameter
}

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.

$("#id_of_anchor").click(function(event) { 
  // ...
  return false;
}

just lose the

href="#"

In the a html tag... that's what causing the web page to "jump up".

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

and remember to add

cursor: pointer;

to your a's css so it will look like a link on mouseover.

Good luck.

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!

        $(document).ready(function() {
        $(".dropdown dt a").click(function(event) {
        event.preventDefault();
            $(".dropdown dd ul").toggle('slow');
        });           
        $(".dropdown dd ul li a").click(function(event) {
        event.preventDefault();
            $(".dropdown dd ul").hide();
        });
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!