jQuery - 点击链接问题触发事件

时间:2023-01-20 19:46:09

I'd like to trigger an event when a link is clicked both by clicking on it normally or by opening it in a new tab (e.g., middle click, ctrl + click, etc)

我想通过正常点击链接或在新标签中打开链接来触发事件(例如,中键,ctrl +点击等)

I've tried the following so far:

到目前为止我已尝试过以下内容:

$('a').click(myfunc) Doesn't capture middle clicks.

$('a')。click(myfunc)不捕获中间点击。

$('a').mousedown(myfunc) works, but it seems to be preventing the link from being followed even though my function doesn't call event.preventDefault.

$('a')。mousedown(myfunc)有效,但它似乎阻止了链接被跟踪,即使我的函数没有调用event.preventDefault。

Any ideas how to do this then?

有什么想法呢?

1 个解决方案

#1


6  

Try returning true from your handler function. Returning nothing can be interpreted by the browser as a void return and thus prevent the default action from being carried out.

尝试从处理函数返回true。浏览器可以将任何返回都解释为void返回,从而阻止执行默认操作。

#1


6  

Try returning true from your handler function. Returning nothing can be interpreted by the browser as a void return and thus prevent the default action from being carried out.

尝试从处理函数返回true。浏览器可以将任何返回都解释为void返回,从而阻止执行默认操作。