jquery触发对焦点或点击操作,但不是两者 [英] jquery trigger action on focus or click but not both
问题描述
我有这个示例代码:
$ myTrigger
.click(function(e){
(b)
$ b b $ b})
目的是当你点击$ myTrigger时,如果,另一方面,你通过键盘(即焦点)选项卡,我想要完全相同的事情发生,所以我要求它点击。
捕获是如果我点击它,它也重点。所以这两个警报都消失了。
有没有办法防止焦点事件在点击时消失?
更新:
Ajm的评论让我想起我可能会问错事。
问题:触发焦点在javascript(和/或在jQuery?)。我可以假设每当我想要处理用鼠标点击和使用键盘插入时,focus()事件将处理这两个?
或者它依赖于我附加事件的特定元素? (在这种情况下$ myObject恰好是一个锚标签(链接)。
jQuery有一个内置函数未使用所有通常称为 .one()
$ mytrigger.one('click focus',function(){alert(event);}) ;
这只会触发一次,或者如果你想要后重新绑定。 >
I have this sample code:
$myTrigger
.click(function(e){
alert('click');
})
.focus(function(e){
alert('focus');
$(this).click()
})
The intent is that I want something to happen when you click on $myTrigger. If, on the other hand, you tab onto it via the keyboard (ie, focus) I want the exact same thing to happen, so I ask it to click.
The catch is if I click on it, it also focuses. So both alerts are going off.
Is there a way to prevent the focus event from going off when clicking?
UPDATE:
Ajm's comment got me thinking that I'm maybe asking the wrong thing.
Question: Does a click event always also trigger focus in javascript (and/or in jQuery?). Can I assume whenever I want to handle both clicking with the mouse and tabbing-in with the keyboard, the focus() event will handle both?
Or is it dependent on the particular element that I'm attaching the events to? (In this case $myObject happens to be an anchor tag (link).
jQuery has a built-in functon for this that's not used all that often called .one()
$mytrigger.one('click focus', function() { alert("event"); });
This will only trigger once, or you can re-bind if you want afterwards.
这篇关于jquery触发对焦点或点击操作,但不是两者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!