Alternative # tag hash symbol tag to empty href?

So, I am working on the site at the moment when there is a jquery onClick function. Well, the link is simple href="#" .

Well, every time it is pressed,

  • The # sign adds a URL
  • The site moves to the top of the page.

Is there a better alternative, or am I doing something wrong?

+4
source share
5 answers

In the click handler, add return false; .

+7
source

The onclick function requires return false . Or you can do this:

 <a href="#" onclick="doSomething();return false;">click</a> 
+3
source

A bit late, but if you use jQuery in the event handler you can use

 $("#myButton").on("click", function(e) { // Whatever the button should do e.preventDefault(); }); 

e.preventDefault() does exactly what prevents the default behavior that would have to scroll to the top of the page (which is the # character itself).

+1
source

The hash following the identifier is a link to the anchor tag with the specified identifier. If the anchor tag does not exist, it can certainly explain why it falls to the top of the page.

0
source

I use this as an alternative

 <a href="javascript:void(0)" onclick="doSomething();return false;">click</a> 
-one
source

Source: https://habr.com/ru/post/1394436/


All Articles