javascript - HTML 元素 : How can I simulate pressing the 'tab' key

标签 javascript jquery tabs focus element

我想触发与在具有焦点的元素上按 Tab 键时相同的效果。有什么办法吗?我试图让焦点跳到下一个元素。

谢谢!

(jQuery 是一个选项)

最佳答案

像这样(使用 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>

关于javascript - HTML 元素 : How can I simulate pressing the 'tab' key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3073264/

相关文章:

javascript - 使用 Angular CLI 和 Angular 5 在运行时动态加载新模块

jquery - 如何在不修改提供的 Rails.js 的情况下覆盖 Rails 3 不显眼的 javascript 函数?

facebook-graph-api - 我可以获得安装了我的 Tab 应用程序的主页列表吗?

javascript - 如何将 jquery stringify 数据附加到具有 4 个用户的可编辑(tr 和 td)数据的表中,如循环

html - 改变 ionic 上的 ion-tabs 高度

ios - React Native 的 React-Navigation : Adding a badge in TabNavigator's Tab

javascript - 如何在 Angular 2 中重用服务调用

javascript - 如何在 3-5 秒后停止加载动画?

javascript - jQuery 使用 map() 从所有输入构建 json

jquery - 将带有新行的文本插入文本区域