Why href=“#” is High Priority than onclick

一曲冷凌霜 提交于 2019-12-24 00:46:00

问题


I usualy found the following code:

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

but sometime my browser go to the top of the page?

Why href="#" is High Priority than onclick?


回答1:


It isn't higher priority. The onclick fires and then the browser follows the link.

If you don't return false (note spelling) or func throws an error (thus not reaching the return statement) the event won't be canceled.

(As fallbacks for if the JS fails or is disabled go, however, a link to the top of the page is really sucky. Progressive enhancement is the way forward.)




回答2:


If the only reason you want to have a href value is to enable the hand cursor, you can use css style instead:

<a style="cursor:pointer;" onclick="func();return false">click</a>


来源:https://stackoverflow.com/questions/4131834/why-href-is-high-priority-than-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!