在句子中的每个单词上移动。
我在我的应用程序中创建了用于输入的快捷键,因为它会移动并聚焦我页面中的每个输入控件。
我需要为选项卡设置键盘快捷键,因为它必须选择某些文本框中的句子的每个字符串。例如 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;
});
关于javascript - 每次按下 TAB 键选择不同的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27146421/