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

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

问题描述

期望的行为:当在网页中的特定 dom 元素上按下 Tabkey 时,我希望我的光标焦点转到地址栏.(我希望通过 Javascript 实现.这里不希望使用任何浏览器扩展)

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

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

解决方案

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

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)

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>

解决方案

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天全站免登陆