Setting events for HTML hyperlinks does not use href to complete the jump

  • 2020-03-30 02:40:30
  • OfStack

Sometimes, we need to use < A> This hyperlink, without using href to complete the jump, is like: < A href = "#" onClick = "fun ()" > < / a> This way. This way, the page won't jump. However, it can also cause some negative problems, such as the title of the page becomes "#", or the page appears scrollbars and so on. This is because after executing the onClick event, < A> It's time to jump to the address that the href points to, and "#" is an anchor, which defaults to the top of the page, so that's where the problem is.

There are two solutions:

1. Add a return false statement to the onClick event, for example:
<a href="#" onClick="fun(); return false;"> Click on the </a> 

2. Replace # with void(0), e.g.
<a href="javascript:void(0)" onclick="fun()"> Click on the </a> 

Related articles: