空链接锚点 [英] Empty-linked anchor

查看:135
本文介绍了空链接锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不会去的地方,但有一个onclick(例如< a onclick =...>< / a> ) 。我的问题属于 href 属性。我应该为这个价值投入什么?

I have an anchor that doesn't go anywhere but has an onclick (e.g. <a onclick="..."></a>). My question pertains to the href attribute. What should I put for the value?

如果我简单地省略 href ,那么锚就没有合适的装饰 - 当鼠标放在它上面时鼠标指针图标不会改变。

If I simply omit href, the anchor doesn't have the appropriate decoration--the mouse pointer icon doesn't change when the mouse is put over it.

如果我使用 href =#

If I use href="#", then the window scrolls to the top of the page when the link is clicked.

如果我使用 href =javascript:...并将 onclick 处理 javascript:,页面将被保留,地址栏包含Javascript单击链接时。

If I use href="javascript:..." and put the value of onclick proceeding javascript:, the page is left and the address bar contains the Javascript when the link is clicked.

当我引用浏览器行为时,我指的是Chrome,我正在测试它。

When I refer to browser behavior, I'm referring to Chrome, which I'm testing it in to start with.

应该怎么做当锚点不是实际的链接,但只存在 onclick 执行行为时,我会为 href

What should I be putting for href when the anchor isn't an actual link but exists only to have the onclick perform behavior?

推荐答案

在您的onclick处理程序中,将此添加到最后:

In your onclick handler, add this to the end:

return false;

这将取消默认处理(即在链接之后),您可以将任何你想要的href(#和所有的一样好)。

This will cancel the default processing (i.e. following the link) and you can put whatever you want in the href (the # is as good as any).

这篇关于空链接锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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