在javascript中模拟tab键功能

pra*_*pal 34 javascript dom-events

我有一张包含大量条目的表格.一旦我在当前文本框中输入值,我想将焦点更改为下一个文本框.并希望将此过程继续到最后一个字段.我的问题是,一旦我在文本框中输入值,是否可以通过javascript编码模拟tab键.

没有按下键盘上的Tab键,我想通过javascript带来相同的功能.这可能吗 ?

krc*_*cko 43

你只需要将焦点放在下一个输入字段上(通过在该输入元素上调用focus()方法),例如,如果你使用jQuery,这个代码将在按下enter时模拟tab键:

var inputs = $(':input').keypress(function(e){ 
    if (e.which == 13) {
       e.preventDefault();
       var nextInput = inputs.get(inputs.index(this) + 1);
       if (nextInput) {
          nextInput.focus();
       }
    }
});
Run Code Online (Sandbox Code Playgroud)

  • @John jQuery是一个JavaScript库,因此任何"jquery代码"也都是有效的JavaScript代码.3年前我已经回答了这个问题,在我的回答中我说"如果你正在使用jQuery".如果有人问我(当时)提供"vanilla js"解决方案,我可能会这样做.我只是不理解像你这样的人(没有违法行为)通过真正的老问题和投票给人们的答案.好像你只是为了好玩而这样做...... (13认同)
  • 我支持我之前的声明,我给出的代码是不言自明的,如果你不使用jQuery,你应该能够理解它背后的概念并将它应用于你选择的库,甚至是vanilla javascript如果你是一个受虐狂(或者你只是有足够的时间来解决很久以前解决的问题). (6认同)
  • 如果jQuery被你过度开发,请不要使用它.这很简单.但请不要喋喋不休地讨论其他开发人员喜欢的工具,并了解使用它的优缺点.当然,jQuery已经变成了相当大的野兽,但是它们正在发生变化,很快你就能够只选择那些你想要使用的jQuery模块(例如DOM遍历和操作).在此之前,您可以在github或[microjs.com](http://microjs.com)上找到大量轻量级库. (2认同)

Joe*_*rra 18

不久前需要模拟选项卡功能,现在我已经将它作为使用的库发布了.

EmulateTab:一个jQuery插件,用于模拟页面元素之间的标签.

您可以在演示中看到它的工作原理.

if (myTextHasBeenFilledWithText) {
  // Tab to the next input after #my-text-input
  $("#my-text-input").emulateTab();
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*sie 13

function nextField(current){
    for (i = 0; i < current.form.elements.length; i++){
        if (current.form.elements[i].tabIndex == current.tabIndex+1){
            current.form.elements[i].focus();
            if (current.form.elements[i].type == "text"){
                current.form.elements[i].select();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当提供当前字段时,它将使用下一个选项卡索引将焦点跳转到该字段.用法如下

<input type="text" onEvent="nextField(this);" />
Run Code Online (Sandbox Code Playgroud)

  • 如果未在 HTML 中手动设置选项卡索引,则不起作用。`.tabIndex` 属性返回 0。 (3认同)

And*_*ers 8

我找不到可以做我想要的答案.我有一个问题,其中有一些我不希望用户标签的链接元素.这就是我想出的:

(请注意,在我自己的代码我用a:not(.dropdown-item)的,而不是aallElements线,为了防止用户tab键a.dropdown-item.)

function(event){
        //Note that this doesn't honour tab-indexes

        event.preventDefault();

        //Isolate the node that we're after
        const currentNode = event.target;

        //find all tab-able elements
        const allElements = document.querySelectorAll('input, button, a, area, object, select, textarea, [contenteditable]');

        //Find the current tab index.
        const currentIndex = [...allElements].findIndex(el => currentNode.isEqualNode(el))

        //focus the following element
        const targetIndex = (currentIndex + 1) % allElements.length;
        allElements[targetIndex].focus();
}
Run Code Online (Sandbox Code Playgroud)


Zac*_*h M 5

这将模拟表单中的选项卡,并在按下Enter键时将焦点放在下一个输入上。

window.onkeypress = function(e) {
    if (e.which == 13) {
        e.preventDefault();
        var inputs = document.getElementsByClassName('input');
        for (var i = 0; i < inputs.length; i++) {
        if (document.activeElement.id == inputs[i].id && i+1 < inputs.length ) {
            inputs[i+1].focus();
            break;   
        }
    }
Run Code Online (Sandbox Code Playgroud)