检测链接何时被激活 - 通过单击或 Tab 并输入

发布于 2024-07-17 13:00:03 字数 113 浏览 5 评论 0原文

只是想知道是否有人知道一种方法,当用户单击链接或链接的选项卡并按 Enter 时,连接 jquery 来运行函数。

我想拦截链接的激活并在页面更改之前执行操作,但我想在任何一种情况下都这样做。

Just wondering if anyone knows of a way that wiring up jquery to run a function for when a user clicks on a link or tabs to a link and hits enter.

I want to intercept that activation of a link and perform an action before the page is changed, but I want to do it in either case.

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

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

发布评论

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

评论(4

渔村楼浪 2024-07-24 13:00:03

在这两种情况下都会触发“click”事件,这将为您提供您想要的:

$('a').click(function(){
    alert('perform action here');
});

The 'click' event will fire in both cases, this will get you what you want:

$('a').click(function(){
    alert('perform action here');
});
沙沙粒小 2024-07-24 13:00:03

实际上非常简单...当在元素上按 Enter 时,它相当于浏览器中的单击。 不需要做任何特别的事情。

$('a.links_to_bind').bind('click', function() { 
    /* do your stuff... */
    return false;
});

编辑:
如果您希望页面在操作完成后发生更改,您可能需要向 return false 添加条件语句。 就像是:

if(everythings_good) {
    return true;
} else {
    return false;
}

It's pretty simple actually... When pressing enter on an element, it acts as a click in browsers. No need to do anything special.

$('a.links_to_bind').bind('click', function() { 
    /* do your stuff... */
    return false;
});

Edit:
If you want the page to change after your actions are complete, you may want to add a conditional statemenet to that return false. Something like:

if(everythings_good) {
    return true;
} else {
    return false;
}
终陌 2024-07-24 13:00:03

以下两个链接提供了有关您要处理的 jQuery 事件的信息。

jQuery 事件/keypress: http://docs.jquery.com/Events/keypress
jQuery 事件/click:http://docs.jquery.com/Events/click

The following two links provide information about the events in jQuery you want to handle.

jQuery Events/keypress: http://docs.jquery.com/Events/keypress

jQuery Events/click: http://docs.jquery.com/Events/click

恬淡成诗 2024-07-24 13:00:03
$('a').keydown(function() {

return false;  // Prevent default action

}); 

$('a').keyup(function(event){

     if(event.keyCode == '13'){

             $(this).find('button.mychoice').click();

     }

}); 


// This does the trick for both IE and FF.
$('a').keydown(function() {

return false;  // Prevent default action

}); 

$('a').keyup(function(event){

     if(event.keyCode == '13'){

             $(this).find('button.mychoice').click();

     }

}); 


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