为什么href =“#"比onclick优先级高 [英] Why href="#" is High Priority than onclick

查看:116
本文介绍了为什么href =“#"比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?

为什么href="#"的优先级高于onclick?

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

推荐答案

它不是更高的优先级.触发onclick,然后浏览器跟随该链接.

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

如果您没有return false(请注意拼写)或func抛出错误(因此未达到return语句),则该事件不会被取消.

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.

(作为JS失败或被禁用的后备工具,但是,指向页面顶部的链接确实很糟糕.

(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.)

这篇关于为什么href =“#"比onclick优先级高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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