如何在输入上创建两个按钮,如Tab(下一个焦点)按键和Shift + Tab(上一个焦点)

时间:2013-04-22 18:21:48

标签: javascript jquery html css

在互联网上搜索2小时后无法找到我想要的内容。

这是我的问题: 如何创建一个按钮,其作用类似于“Tab键”和“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>

抱歉我的问题和英语不好

1 个答案:

答案 0 :(得分:1)

如果您首先将input的{​​{1}}存储到focus而不是非常简单:

LIVE DEMO

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 符号括号。

我们只是确保dotprev元素实际上是next使用:

input