admin管理员组文章数量:1435859
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?
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?
3 Answers
Reset to default 4It 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.)
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>
...and a decade later, here's the answer to the question asked above;
<a href="#" onclick="event.preventDefault(); func()">click</a>
To save confusion, this answer does not address the "return false" aspect. That subject can be researched separately.
PS; Technically there is no hierarchy (AKA "priority") for what happens when an Anchor Tag is clicked. But since HREF is the "default" action / event for an anchor tag, from a certain perspective, HREF does "take priority" over OnClick.
本文标签: javascriptWhy hrefquotquot is High Priority than onclickStack Overflow
版权声明:本文标题:javascript - Why href="#" is High Priority than onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745668648a2669434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论