onMousedown 与 onClick 的缺点?

发布于 2024-08-09 08:01:02 字数 483 浏览 4 评论 0原文

我一直在处理一个致命的 Javascript 问题,涉及跟踪用户何时单击链接(如果您好奇,这里是: 为什么使用 target="_blank" 会导致 Javascript 失败?)。

我发现可以通过跟踪 onMousedown 事件而不是 onClick 事件来解决问题。

我很好奇这种方法的缺点。我能想到的:

  1. 如果用户单击链接,然后在释放之前将鼠标从链接上移开,那么即使用户没有访问该链接,也会记录该事件
  2. 如果用户使用 Tab 键将浏览器焦点移至链接,然后按 Enter 键,将不会记录单击。

这两种情况都不常见,因此我并不非常担心它们。

我还缺少其他缺点吗?

I've been dealing with a bane-of-my-existence Javascript problem involving tracking when a user clicks on a link (in case you're curious, here it is: Why does using target="_blank" cause Javascript to fail?).

I've figured out that I can solve the problem by tracking an onMousedown event rather than an onClick event.

I'm curious about the downsides of this approach. The ones I can think of:

  1. If a user clicked down on a link and then moved the mouse off the link before releasing it, then the event would be recorded even though the user hadn't visited the link
  2. If a user used the tab key to move the browser focus to the link and then hit enter, the click would not be recorded

Neither of these are common, so I'm not terribly worried about them.

Are there any other downsides I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2024-08-16 08:01:02

还有一件事:鼠标按下也可以捕获右键/中键单击。

但由于你的两个原因,我会坚持使用 onclick。我认识很多使用键盘导航的人。特别是 FF 中的 search-and-gotolink(/ 进行搜索,然后输入进入链接)。

但如果这两个对你来说不是问题,我认为右键/中键点击也不会太麻烦。

我认为跟踪所有点击链接的用户的方法非常棘手——用户可以右键单击并单击新选项卡/新窗口...

One more: mousedown captures right / middle clicks too.

But for your two reasons, I would stick to onclick. I know quite a few people who use keyboard nav. Especially search-and-gotolink in FF.(/ to search followed by enter to go to the link).

But if these two are not a problem for you, I think right / middle clicks wouldn't be too.

I think the way to track all the users who follow the link is quite tricky -- the user could right click and click on new tab / new window...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文