admin管理员组文章数量:1356413
I've got an HTML link, and I want to take some action when the user tabs away from it - but only if the user is tabbing forwards through the document, as opposed to backwards.
Is there a reliable cross-browser way to detect which way the user is tabbing through the document, or indeed if they're tabbing through the document at all? I'm binding to the blur
event, but that doesn't necessarily mean that the user is tabbing.
I've had a look at inspecting the value of document.activeElement
, or the hasFocus
attribute of the previous focusable element in the source, but:
- those seem like relatively recent additions, and thus might not be widely supported, and
- I'm not sure they'll be inspectable when the
blur
event fires, as even if the user is tabbing, I don't think the next element will be focused yet.
I've got an HTML link, and I want to take some action when the user tabs away from it - but only if the user is tabbing forwards through the document, as opposed to backwards.
Is there a reliable cross-browser way to detect which way the user is tabbing through the document, or indeed if they're tabbing through the document at all? I'm binding to the blur
event, but that doesn't necessarily mean that the user is tabbing.
I've had a look at inspecting the value of document.activeElement
, or the hasFocus
attribute of the previous focusable element in the source, but:
- those seem like relatively recent additions, and thus might not be widely supported, and
- I'm not sure they'll be inspectable when the
blur
event fires, as even if the user is tabbing, I don't think the next element will be focused yet.
- It's very peculiar of you to ask such questions since your reputation is rather high and especially because you have Javascript, HTML and jQuery badges... I'm a bit baffled. :S – Robert Koritnik Commented May 26, 2011 at 11:24
- 2 What do you want to do in response to it? There's a general accessibility guideline that you should never 'change context' (eg. move focus elsewhere, navigate to a different page) in response to focus change - regardless of direction. The right thing to do here may depend on what you plan on doing in response to the focus change... – BrendanMcK Commented May 27, 2011 at 9:42
- 1 @BrendanMcK: sure — I’ve since decided to avoid such detailed fiddling with the user’s keyboard actions. I think the whole thing was ill-conceived. – Paul D. Waite Commented May 31, 2011 at 9:03
2 Answers
Reset to default 8You will have to handle keydown
event on the link itself.
$("your link selector").keydown(function(evt){
if (evt.which === 9)
{
if(evt.shiftKey === true)
{
// user is tabbing backward
}
else
{
// User is tabbing forward
}
}
});
Check this JSFiddle example that detects forward and backward tabbing on a particular link.
Sidenote: You didn't specify jQuery tag on your question al though I provided jQuery code in my answer. Since you hold Javascript as well as jQuery badges I suppose it's going to be trivial for you to convert my code to pure Javascript.
As an alternative to a good solution from Robert, maybe you can leverage tabindex attribute? Set it for your html links and other “tabbable” items. Then check it in javascript.
Solution with tabindex: jsfiddle
Side effect: Elements will also react on mouse clicks. They will behave correctly. So this might be a good side effect or bad side effect depending on your needs.
本文标签: keyboardUsing JavaScripthow can you tell if a user is tabbing backwardsStack Overflow
版权声明:本文标题:keyboard - Using JavaScript, how can you tell if a user is tabbing backwards? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744065495a2584914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论