te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Take my web page focus to browser address bar using javascriptjquery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Take my web page focus to browser address bar using javascriptjquery - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question edited Jul 25, 2017 at 13:45 Rohit Kumar asked Jul 25, 2017 at 13:27 Rohit KumarRohit Kumar 8492 gold badges14 silver badges22 bronze badges 11
  • 3 Possible duplicate of Highlight URL bar using Chrome and Safari extension – Ritesh Nair Commented Jul 25, 2017 at 13:29
  • 2 No. I want to achieve it using Javascript/jquery. Browser plugin is not the solution – Rohit Kumar Commented Jul 25, 2017 at 13:47
  • 2 This isn't possible. – Daniel Beck Commented Jul 25, 2017 at 14:01
  • 1 Some people can't use a mouse and rely on the keyboard to move around the page. The TAB key is an essential navigation key; catching TAB key presses and sending focus to the URL bar (if it is technically possible) would break navigation for this group of users. In addition, I can't even think of a reason for doing this. – Tsundoku Commented Jul 26, 2017 at 10:33
  • 1 Thanks for the clarification, but it's still not clear to me why it is necessary for the plugin to give focus to the URL bar when users should be able to do this without the help of a plugin or application. – Tsundoku Commented Jul 27, 2017 at 11:33
 |  Show 6 more ments

2 Answers 2

Reset to default 11

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 mands differently (if they're not different already). For example, on a mac the mand to access the address bar is Command + L, not Ctrl + L.

According to Trusted events in the UI Events specification

Events that are generated by the user agent, either as a result of user interaction, or as a direct result of changes to the DOM, are trusted by the user agent with privileges that are not afforded to events generated by script through the createEvent() method, modified using the initEvent() method, or dispatched via the dispatchEvent() method. The isTrusted attribute of trusted events has a value of true, while untrusted events have a isTrusted attribute value of false.

So triggering the CTRL+L event will have no effect on your browser which will need a trusted event for opening the address bar.

And yes, as already said by some people, removing default browser behaviour does not help accessibility.


On a purely technical point of view, jQuery UI gives you a very quick way for answering the use case in your ments. This will move to the last focusable element on "keydown", and then when you relapse the tab key, will go to the default element after your page (address bar, depending on your browser)

$("#MyLastElement").on('keydown', function (e) {
  if((e.keyCode==9)&&(!e.shiftKey)) {
     $(":tabbable").last().focus();
  }
});
发布评论

评论列表(0)

  1. 暂无评论