使用javascript/jquery将我的网页焦点移到浏览器地址栏 [英] Take my web page focus to browser address bar using javascript / jquery

查看:147
本文介绍了使用javascript/jquery将我的网页焦点移到浏览器地址栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

期望的行为:当在网页中的特定dom元素上按下Tab键时,我希望光标焦点移至地址栏. (我想通过Javascript来实现.这里不需要任何浏览器扩展)

Desired behavior : When a Tabkey press happens on a particular dom element in a webPage I want my cursor focus to go to address bar. (I want it via Javascript. Using any browser extension is not what is desired here)

在网页中按Control + L快捷键时,会将您的焦点移到地址栏. 但是,当我尝试通过javascript触发它时,它不起作用.

When you press Control + L shortcut in a webpage, it takes your focus to address bar. But when I try to trigger this via javascript it does'not work.

<div id="1" tabindex="1"></div>
<div id="2" tabindex="1"></div>
<div id="3" tabindex="1"></div>
<script>
    var some = $('#1');
    some.on('keydown', function (e) {
        if (e.keyCode == 9 /* TabKey */) {
            var e = jQuery.Event("keydown");
            e.keyCode = 76 /* LKey */;
            e.ctrlKey = true;
            $('body').trigger(e);

        }
    });
</script>

推荐答案

每个浏览器(和操作系统)对此的处理方式都不相同,目前无法使用javascript突出显示地址栏.即使是这样,请记住,人们可以以不同的方式映射这些命令(如果它们已经不同的话).例如,在Mac上,用于访问地址栏的命令为Command + L,而不是Ctrl + L.

Each browser (and operating system) handles this differently and it is not currently possible to highlight the address bar using javascript. Even if it was, keep in mind that people can map these commands differently (if they're not different already). For example, on a mac the command to access the address bar is Command + L, not Ctrl + L.

这篇关于使用javascript/jquery将我的网页焦点移到浏览器地址栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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