开发者

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

开发者 https://www.devze.com 2023-01-12 15:40 出处:网络
开发者_Go百科I have an anchor tag <a href=\"#\" onclick=\"Register();\" >Register</a>
开发者_Go百科

I have an anchor tag

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

If you don't have a href in you anchor, it can screw up some stuff, but I don't need to redirect, just call a js function. When this link is clicked however, the href="#" causes it to scroll the page to the top. How do I prevent that from happening?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号