2

フォーカスのある要素でタブキーを押したときと同じ効果をトリガーしたい。これを行う方法はありますか?フォーカスを次の要素にジャンプさせようとしています。

ありがとう!

(jQueryはオプションです)

4

2 に答える 2

1

このようなもの(jQueryを使用):

    <!DOCTYPE html>

    <html lang="en">

        <head>

            <title>LOL Focus!</title>

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>

        </head>

        <form>
          <input class="focusable" type="text" value="lol"/>
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />                 
        </form> 

        <input type="button" id="trigger_button" value="lol" />

        <script type="text/javascript">
        $(function() {

          var focusable = $('.focusable'),
              last_element_index = focusable.length - 1,
              current_index;

          focusable.each(function(i) {
            $(this).click(function() {
              current_index = i;
            })
          });

           $('#trigger_button').click(function() {
             current_index = (should_reset_current_index()) ? 0 : current_index + 1;
             focusable[current_index].focus();
           });

           function should_reset_current_index() {
             return (typeof(current_index) === 'undefined') || (current_index == last_element_index)
           }

        });
      </script>

    </html>
于 2011-09-08T06:06:04.383 に答える
0

しばらく前にタブ機能をエミュレートする必要があり、今回はを使用するライブラリとしてリリースしました

EmulateTab : ページ上の要素間のタブ移動をエミュレートする jQuery プラグイン。

デモでその仕組みを確認できます。

if (myTextHasBeenFilledWithText) {
  // Tab to the next input after #my-text-input
  $("#my-text-input").emulateTab();
}
于 2012-02-17T11:16:23.297 に答える