2013-04-22 57 views
1

在Internet上2小时后搜索找不到我想要的内容。如何在输入上创建两个按钮,如Tab(下一个焦点)按键和Shift + Tab(上一个焦点)

这里是我的问题: 如何创建一个像“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> 

对不起糟糕表现,我的问题和英语

+0

你有什么期望时,目前的重点是'#4'什么关系呢?跳转到“Tab”按钮? – Bergi 2013-04-22 18:27:40

+0

在页面上有许多输入,所以如果它持续一个,那么它可以返回到第一个输入,这对我很有好处,因为我在一个页面上有50个输入,所以它会用一个自定义焦点跳线长输入页面。 – 2013-04-22 18:47:08

+0

感谢您向我展示其他来源,但我在编码方面真的很新颖。 any1可以写简单的应付例子吗? – 2013-04-22 18:58:11

回答

0

如果您先存储有focusvar比它的input真的很简单:

LIVE DEMO

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 

只使用了支架表示法。

比我们只是确保prevnext元素实际上是一个input使用:

('input').focus(); // and set the focus to it! 
+0

非常感谢你 – 2013-04-22 19:57:17

+0

当我把它们放在一些ul div中时,它们专注于不工作 链接:http://jsbin.com/akabac/5/编辑 请帮忙 – 2013-04-22 21:06:22

+0

@MikeyXuan它不起作用因为现在'prev'和'next'没有任何更多的输入,但没有什么原因,现在它们被分隔在'li'元素中。 – 2013-04-22 21:11:08

相关问题