Anchor tag with onclick and href='#' scrolls to top

后端 未结 2 1983
心在旅途
心在旅途 2021-01-17 12:14

I have an anchor tag

Register

If you don\'t have a href in you anchor, it can screw

相关标签:
2条回答
  • 2021-01-17 12:23

    You can add a return false;, like this:

    <a href="#" onclick="Register(); return false;">Register</a>
    

    This prevents the default action of the anchor, which is to go to the hash, causing the scroll.

    0 讨论(0)
  • 2021-01-17 12:41

    Use:

     <a href="javascript:void(0);" onclick="Register(); ">Register</a>
    

    javascript:void(0) will prevent the page from getting redirected and also will fix the issue where your page is getting scrolled on top due to href="#"

    0 讨论(0)
提交回复
热议问题