1. <small id='y2rBA'></small><noframes id='y2rBA'>

    <tfoot id='y2rBA'></tfoot>
  2. <legend id='y2rBA'><style id='y2rBA'><dir id='y2rBA'><q id='y2rBA'></q></dir></style></legend>

      <i id='y2rBA'><tr id='y2rBA'><dt id='y2rBA'><q id='y2rBA'><span id='y2rBA'><b id='y2rBA'><form id='y2rBA'><ins id='y2rBA'></ins><ul id='y2rBA'></ul><sub id='y2rBA'></sub></form><legend id='y2rBA'></legend><bdo id='y2rBA'><pre id='y2rBA'><center id='y2rBA'></center></pre></bdo></b><th id='y2rBA'></th></span></q></dt></tr></i><div id='y2rBA'><tfoot id='y2rBA'></tfoot><dl id='y2rBA'><fieldset id='y2rBA'></fieldset></dl></div>
      • <bdo id='y2rBA'></bdo><ul id='y2rBA'></ul>

    1. 使用 JavaScript 模拟 tab 按键

      时间:2023-09-07
    2. <i id='wsy0J'><tr id='wsy0J'><dt id='wsy0J'><q id='wsy0J'><span id='wsy0J'><b id='wsy0J'><form id='wsy0J'><ins id='wsy0J'></ins><ul id='wsy0J'></ul><sub id='wsy0J'></sub></form><legend id='wsy0J'></legend><bdo id='wsy0J'><pre id='wsy0J'><center id='wsy0J'></center></pre></bdo></b><th id='wsy0J'></th></span></q></dt></tr></i><div id='wsy0J'><tfoot id='wsy0J'></tfoot><dl id='wsy0J'><fieldset id='wsy0J'></fieldset></dl></div>

            <bdo id='wsy0J'></bdo><ul id='wsy0J'></ul>

            <small id='wsy0J'></small><noframes id='wsy0J'>

              <tbody id='wsy0J'></tbody>
          • <tfoot id='wsy0J'></tfoot>

            <legend id='wsy0J'><style id='wsy0J'><dir id='wsy0J'><q id='wsy0J'></q></dir></style></legend>

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

                问题描述

                我想让浏览器的行为就像用户在点击某物时按下了 Tab 键一样.在点击处理程序中,我尝试了以下方法:

                I'd like to have the browser act as if the user had pressed the Tab key when they click on something. In the click handler I've tried the following approaches:

                var event = document.createEvent('KeyboardEvent');
                event.initKeyEvent("keypress", true, true, null, false, false, false, false, 9, 0);
                this.input.focus()[0].dispatchEvent(event);
                

                还有 jQuery:

                this.input.focus().trigger({ type : 'keypress', which : 9 });
                

                ...我取自这里.

                第一种方法似乎是最好的选择,但不太奏效.如果我将最后两个参数更改为 98、98,实际上,在输入框中输入了一个b".但是 9, 0 和 9, 9(我直接从 MDC 网站获取的前者)在 FF3 下的 firebug 中都给了我这些错误:

                The first approach seems to be the best bet, but doesn't quite work. If I change the last two parameters to 98, 98, indeed, a 'b' is typed into the input box. But 9, 0 and 9, 9 (the former of which I took right from the MDC web site) both give me these errors in firebug under FF3:

                Permission denied to get property XULElement.popupOpen
                [Break on this error] this.input.focus()[0].dispatchEvent(event);
                
                Permission denied to get property XULElement.overrideValue
                [Break on this error] this.input.focus()[0].dispatchEvent(event);
                
                Permission denied to get property XULElement.selectedIndex
                [Break on this error] this.input.focus()[0].dispatchEvent(event);
                
                Permission denied to set property XULElement.selectedIndex
                [Break on this error] this.input.focus()[0].dispatchEvent(event);
                

                我听说此类(没有明确定义此类")事件是不受信任的",这可能解释了这些错误.

                I've heard such (with no clear definition of 'such') events are 'untrusted', which might explain these errors.

                第二种方法会导致我作为 event.which 输入的任何值都作为 event.which 传递,但没有效果(即使我使用 98 而不是 9,也不会在框中输入b".)如果我尝试在我传递的对象中设置 event.data,当事件被触发时,它最终是未定义的.以下是我用来查看的代码:

                The second approach causes whatever value I put as event.which to be passed as event.which, but to no effect (even if I use 98 instead of 9, no 'b' is typed in the box.) If I try setting event.data in the object I'm passing, it ends up undefined when the event is triggered. What follows is the code I'm using to view that:

                $('#hi').keypress(function(e) {
                  console.log(e);
                });
                

                还有其他想法吗?

                推荐答案

                我最终采用的解决方案是创建一个焦点窃取器" div(tabindex = -1--可以有焦点但不能选项卡最初)在我想要手动管理焦点的区域的任一侧.然后我放置了一个冒泡的真实事件监听器,用于对整个区域进行聚焦和模糊处理.当该区域发生任何焦点时,tabindex 值将更改为 -1,当发生任何模糊时,它们将更改为 0.这意味着当焦点在该区域时,您可以使用 tab 或 shift-tab 离开它并且正确地结束在其他页面元素或浏览器 UI 元素上,但是一旦您离开那里,焦点窃取器就会变成可选项卡,并且在焦点上它们正确设置手动区域并将焦点转移到最后的元素上,就像您单击了手动区域的一端或另一端一样.

                The solution I ended up going with is to create a "focus stealer" div (with tabindex = -1--can have the focus but can't be tabbed to initially) on either side of the area in which I want to manually manage the focus. Then I put a bubbling-true event listener for focus and blur on the whole area. When any focus occurs on the area, the tabindex values are changed to -1, and when any blur occurs, they're changed to 0. This means that while focused in the area, you can tab or shift-tab out of it and correctly end up on other page elements or browser UI elements, but as soon as you focus out of there, the focus stealers become tabbable, and on focus they set up the manual area correctly and shunt the focus over to the element at their end, as if you had clicked on one end or the other of the manual area.

                这篇关于使用 JavaScript 模拟 tab 按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:以编程方式触发 Ctrl+S 下一篇:在 HTML5 画布中创建关键事件的最佳方式是什么

                相关文章

                最新文章

              1. <tfoot id='fiZ3P'></tfoot>

                <small id='fiZ3P'></small><noframes id='fiZ3P'>

                <legend id='fiZ3P'><style id='fiZ3P'><dir id='fiZ3P'><q id='fiZ3P'></q></dir></style></legend>

                    • <bdo id='fiZ3P'></bdo><ul id='fiZ3P'></ul>

                    <i id='fiZ3P'><tr id='fiZ3P'><dt id='fiZ3P'><q id='fiZ3P'><span id='fiZ3P'><b id='fiZ3P'><form id='fiZ3P'><ins id='fiZ3P'></ins><ul id='fiZ3P'></ul><sub id='fiZ3P'></sub></form><legend id='fiZ3P'></legend><bdo id='fiZ3P'><pre id='fiZ3P'><center id='fiZ3P'></center></pre></bdo></b><th id='fiZ3P'></th></span></q></dt></tr></i><div id='fiZ3P'><tfoot id='fiZ3P'></tfoot><dl id='fiZ3P'><fieldset id='fiZ3P'></fieldset></dl></div>