用于处理Tab键按下的JavaScript [英] JavaScript for handling Tab Key press

查看:120
本文介绍了用于处理Tab键按下的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,当我们点击键盘上的 TAB 键时,它允许我们浏览当前打开网页的所有活动href链接。是否可以通过JavaScript读取这些网址?

As we know, when we click on TAB key on keyboard, it allows us to navigate through all active href links present open webpage. Is it possible to read those urls by means of JavaScript?

示例:

function checkTabPress(key_val) {
    if (event.keyCode == 9) {
        // Here read the active selected link.
    }
}


推荐答案

你应该能够使用keyup事件执行此操作。具体来说, event.target 应指向所选元素,而 event.target.href 将为您提供href - 该元素的价值。有关详细信息,请参见 mdn

You should be able to do this with the keyup event. To be specific, event.target should point at the selected element and event.target.href will give you the href-value of that element. See mdn for more information.

以下代码是jQuery,但除了样板代码外,其余代码在纯javascript中是相同的。这是绑定到每个链接标记的 keyup 处理程序。

The following code is jQuery, but apart from the boilerplate code, the rest is the same in pure javascript. This is a keyup handler that is bound to every link tag.

$('a').on( 'keyup', function( e ) {
    if( e.which == 9 ) {
        console.log( e.target.href );
    }
} );

jsFiddle: http: //jsfiddle.net/4PqUF/

jsFiddle: http://jsfiddle.net/4PqUF/

这篇关于用于处理Tab键按下的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆