锚点链接中的Href和onclick问题 [英] Href and onclick issue in anchor link

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

问题描述

我有指定href和onclick事件的锚点链接。 onclick将location.href设置为服务器url,将href设置为其他链接。
单击链接时,将始终调用href事件,但只调用onclick事件一次(仅适用于一个锚链接)。如果我调试javascript它正常工作,它调用href和onclick.What可能是什么问题?

I have anchor link with both href and onclick events specified. The onclick sets location.href to server url and href to a other link. When the link is clicked the href event gets called always but onclick event gets called only once (that is only for one anchor link). If i debug javascript it works properly that it calls both href and onclick.What can be the problem?

推荐答案

尝试

 <a href="whatever.html" onclick='window.location.href="http://whereverelse";return false;'>linky</a>

只是为了解释:在这种情况下返回false会阻止默认操作。

just to explain: returning false in this case prevents the default action.

你为什么要这样做?

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

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