如何在输入上创建两个按钮,如Tab(下一个焦点)按键和Shift+Tab(上一个焦点)
How to create two button acts like Tab(next focus) keypress and Shift+Tab(previous focus) on inputs
在网上搜索了2个小时后找不到我想要的东西。
我的问题是:我如何创建一个按钮,它的行为像"Tab键"answers"Shift + Tab键"它聚焦下一个输入文本或以前和第一次按将聚焦第一次输入。像这样:
<div class="inputs">
<input type="text" tabindex="1"/>
<input type="text" tabindex="2" />
<input type="text" tabindex="3" />
<input type="text" tabindex="4" />
<button>Tab(Go next input to Focus)</button>
<button>Shift + Tab (fo Previous input to focus)</button>
</div>
很抱歉,我的问题和英语呈现得不好
如果您先将具有focus
的input
存储到var
中,那么它真的很简单:
现场演示
var $curr;
$('.inputs input').on('focus',function(){
$curr = $(this);
});
$('.prevTab, .nextTab').on('click', function( e ){
$curr[ $(this).hasClass("nextTab") ? "next" : "prev" ]('input').focus();
});
更复杂的那行
$curr.prev('input').focus();
或$curr.next('input').focus();
取决于使用简单的三元运算符逻辑单击哪个按钮:
if .nextTab is clicked ["next"] will be used
if .prevTab is clicked ["prev"] will be used
考虑到符号
$curr["next"]() is the same as $curr.next() method
$curr["prev"]() is the same as $curr.prev() method
只是用括号代替dot
符号。
我们只是确保prev
或next
元素实际上是input
使用:
('input').focus(); // and set the focus to it!
相关文章:
- keyup事件处理程序更改焦点不适用于快速键入
- 单击时将焦点更改为元素
- 如何在使用jQuery按下TAB键后返回焦点
- Ace 编辑器将字段焦点放在 Tab 键上
- 带键盘的 j轮播 :使用 Tab 键的错误焦点
- 使用纯JS或jQuery通过Alt Tab检测浏览器页面窗口何时失去焦点
- 使用tab键更改焦点颜色的按钮,并在按enter键时返回原始颜色
- 是否有一种方法可以通过编程触发TAB键将焦点移动到下一个可聚焦的元素
- Jquery-ui自动完成防止TAB键焦点
- 即使在javascript中设置了焦点之后,Tab事件也会改变焦点
- TAB焦点没有正确显示
- 为什么不模拟tab键将焦点移到下一个输入字段?
- 如何在输入上创建两个按钮,如Tab(下一个焦点)按键和Shift+Tab(上一个焦点)
- 为什么不';preventDefault()在按下tab键后停止焦点变化?
- 如何取消选择文本框的文本,当它得到焦点时,当tab被按下
- 使用tab按钮避免焦点超出特定按钮
- 通过jquery在Tab键上按下焦点到图像按钮不起作用
- jQuery选择插件:捕获焦点以在Tab时打开
- 在asp.net中输入Key作为Tab并将焦点移动到下一个控件
- 为什么tab键会引起焦点变化,也会触发keyup事件