onClick 是通用的鼠标按钮吗?
我注意到 Google 在其搜索结果中使用 onMouseDown - 用于网络跟踪、查看关键字和排名等。
我想知道 onClick 或 onMouseDown 哪个更好 - 或者它们都支持以下所有功能:中键、左键单击、右键单击(其他按钮如玩家鼠标)。所有浏览器(包括手机、平板电脑和所有其他操作系统上的浏览器)是否都支持它们?最重要的是该函数首先加载。 HREF 留给 SEO 和其他 UI/UX 好处,以防 javascript 失败。
<a href="http://www.site.com" onclick="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>
或者
<a href="http://www.site.com" onMousedown="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>
或者看似显而易见的(我不会这样做,因为空间的每个字符对我的客户都很重要)< /em>
<a href="http://www.site.com" onclick="doMyFunctionFirst();" onMouseDown="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>
I notice Google use onMouseDown in their search results - for web tracking, seeing keyword and ranking etc.
I want to know which is better, onClick or onMouseDown - or do they both support all of the following: middle button, left click, right click (other buttons like Gamer mouse). Are they supported in all browsers including those on mobile phones, tablets and all other operating systems? It is paramount that the function loads first. HREF is left for SEO and other UI/UX benefits in case of failure of javascript.
<a href="http://www.site.com" onclick="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>
Or
<a href="http://www.site.com" onMousedown="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>
Or the seemingly obvious (I do not do this as every character for space matters for my client)
<a href="http://www.site.com" onclick="doMyFunctionFirst();" onMouseDown="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
值得注意的是,如果您按 Tab 键切换到链接并按 Enter 键,则会触发
click
事件,但mousedown
/mouseup
不会。Notably, the
click
event will fire if you tab to a link and press Enter, but themousedown
/mouseup
will not.主要区别在于,无论按下哪个鼠标按钮,
onmousedown
都会触发,而onclick
仅针对鼠标左键触发 - 这很可能是您想要的。The main difference is that while
onmousedown
triggers regardless of which mouse-button was pressed,onclick
triggers only for the left mouse button - which will be most likely what you want.W3C 规范 似乎没有指定 是否应为所有按钮触发事件,仅应在事件对象的“button”属性上指示按下的按钮。至少在 Chrome 下,除了主鼠标按钮(在我的情况下,左键)之外,“单击”似乎不会触发任何操作。 Quirksmode 有一个有用的测试工具来检查这一点。
onclick、onmousedown 和 onmouseup 代表不同的行为,没有一个本质上是“更好”的:“单击”是按下然后释放按钮,mousedown 是“按下”,mouseup 是“释放”。通常,如果您正在进行一些拖/放操作,这将是相关的。
MDN 文档指出 onclick 在 mousedown 和 mouseup 之后触发。
另外,理想情况下,您应该使用 Unobtrusive Javascript 技术而不是在标记中附加事件处理程序。
如果事件处理程序正在执行重定向/AJAX 请求或类似请求,则应使用 preventDefault 方法。
The W3C spec doesn't seem to specify whether the events should fire for all buttons, only that the button pressed should be indicated on the "button" property of the event object. Under Chrome at least it doesn't appear that "click" fires for anything except the main (in my case, left) mouse button. Quirksmode has a useful test harness to check this.
The onclick, onmousedown and onmouseup represent different behaviours, none of which is intrinsicly "better": a "click" is a press and then release of the button, mousedown is the "down" and mouseup is the "release". Normally this would be relevant if you were doing some drag/drop operations.
The MDN documentation indicates that onclick fires after mousedown and mouseup.
Also, ideally you should be attaching event handlers using Unobtrusive Javascript techniques, rather than in the markup.
If the event handler is doing a redirect/AJAX request or similar, you should use the preventDefault method if you don't want the browser to follow the link after firing your event.