jquery click on anchor element forces scroll to to

2019-02-01 22:07发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.



回答5:

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


回答6:

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.



回答7:

I've been having the same problem.

I think it occurs when you bind the event before appending the element to the DOM.



回答8:

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