带onclick和href ='#'的锚标记滚动到顶部 [英] Anchor tag with onclick and href='#' scrolls to top

查看:66
本文介绍了带onclick和href ='#'的锚标记滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定位标记

 < a href =#onclick =Register(); >注册< / A> 

如果你没有href,你可能会搞砸一些东西,但是我不需要重定向,只需调用一个js函数即可。然而,当点击此链接时,href =#会导致它将页面滚动到顶部。如何防止这种情况发生?

解决方案

您可以添加 return false; ,如下所示:

 < a href =#onclick =Register(); return false; >注册< / A> 

这样可以防止锚点的默认动作,即导致滚动的散列。

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.

这篇关于带onclick和href ='#'的锚标记滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆