每次按 TAB 键时选择不同的单词

Select different word each time TAB key is pressed

本文关键字:单词 选择 TAB      更新时间:2023-09-26

在句子中的每个单词上移动。

我已经为在我的应用程序中创建了输入的快捷键,因为它将移向并聚焦我的页面中的每个输入控件。

我需要为选项卡设置键盘快捷键,因为它必须选择某个文本框中的句子的每个字符串。例如,txtAddress 包含像"嗨,我是新用户"这样的值,如果我按 Tab 键,它必须选择一个字符串"hi"然后是"i"然后是"am",然后是"new"然后是"user",然后它必须聚焦下一个输入控件。

我尝试按照JS来关注下一个输入控件,但不知道如何选择文本框中的每个单词。

$(document).unbind('keydown');
$(document).bind('keydown', 'tab', function assets() {
    try {
        var inputs = $(":input:not(input[type='hidden'])");
        var CurInput = inputs.get(inputs.index(document.activeElement));
        var nextInput = inputs.get(inputs.index(document.activeElement) + 1);
        if (CurInput && nextInput.type == "text" && CurInput.style.display != "none") {
            var strval = CurInput.value;
            if (!strval) {
                if (nextInput && nextInput.type != "hidden" && nextInput.style.display != "none") {
                    nextInput.focus();
                }
            }
        }
        else if (nextInput && nextInput.type != "hidden" && nextInput.style.display != "none") {
            nextInput.focus();
        }
        return false;
    }
    catch (e) {
    }
});

http://jsbin.com/cihahigevo/1/edit?html,js,output

var textarea = $('textarea')[0],
    index = 0;
$(document).on('keydown.tab', function(e){
  if( e.keyCode == 9 ){
    textarea.focus();
    textarea.value = textarea.value.trim() + ' ';
    index = textarea.value.indexOf(' ', index) + 1;
    textarea.setSelectionRange(0, index);
  }
  return false;
});

覆盖用户键盘从来都不是一个好主意,尤其是选项卡按钮。

选项卡按钮由

(无论出于何种原因)不使用鼠标通过在按钮、表单字段等之间"按 Tab 键"来导航网站的人使用。

如果您通过覆盖 Tab 键来删除此功能,则会突然使这些用户无法访问您的网站。

您也可能违反您所在国家/地区有关网站可访问性的法律(英国的《残疾与歧视法》)。