1

I have a hash hyperlink that is used to show specific jquery based on the hash given.

The problem is that the browser automatically scrolls to the DOM element that has the class or id...how can we stop this?

4

2 回答 2

4
  • Don't give your element that id
  • Or call .preventDefault() in the click event of the link

    $("a").click(function(e) {
        e.preventDefault()
    });
    
于 2013-06-19T13:48:12.983 回答
1

For that, you should use:

preventDefault();

The event.preventDefault() method stops the default action of an element from happening. For example: Prevent a submit button from submitting a form Prevent a link from following the URL

Find more about it @ w3schools : event.preventDefault()

Usage:

$("a").click(function(event){
  event.preventDefault();
});
于 2013-06-19T13:51:59.153 回答