【问题标题】:How to create two button acts like Tab(next focus) keypress and Shift+Tab(previous focus) on inputs [duplicate]如何在输入上创建两个按钮,例如 Tab(下一个焦点)按键和 Shift+Tab(上一个焦点)[重复]
【发布时间】:2013-04-22 18:21:47
【问题描述】:

在互联网上搜索 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>

抱歉,我的问题和英语表达不好

【问题讨论】:

  • 当当前焦点位于#4 时,您希望它做什么?跳转到Tab 按钮?
  • 页面上有很多输入,所以如果它是最后一个,那么它可以返回到第一个输入,这对我有好处,因为我在一个页面上有 50 个输入,所以它会很长的输入页面带有自定义焦点跳线。
  • 感谢您向我展示其他资源,但我对编码真的很陌生。任何人都可以写简单的应对例子吗?
  • 投票支持重新开放,因为 OP 需求与建议的“克隆”不同

标签: javascript jquery html css


【解决方案1】:

如果您首先将具有focusinput 存储到var 中,那真的很简单:

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

只使用插入dot 符号的括号。

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

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

【讨论】:

  • 当我把它们放在一些 ul div 中时,它们的焦点不起作用链接:jsbin.com/akabac/5/edit 请帮助
  • @MikeyXuan 它不起作用,因为现在 prevnext 不再是输入,但现在它们在 li 元素中被分开了。
  • @MikeyXuan 现在使用这条线:$curr.closest('li')[ $(this).hasClass("nextFocus") ? "next" : "prev" ]('li').find('input').focus();
  • @MikeyXuan 这里是一个活生生的例子:jsbin.com/akabac/7/edit
猜你喜欢
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多