1

インターネットで 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

1 に答える 1

1

最初に を含む を に保存する場合、それinputは本当に簡単です:focusvar

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表記の代わりに括弧を使用するだけです。

prevornext要素が実際にinputusingであることを確認しているだけではありません。

 ('input').focus(); // and set the focus to it!
于 2013-04-22T19:38:10.413 に答える