页面加载时触发选项卡按下键事件 [英] trigger tab press key event on page load

查看:78
本文介绍了页面加载时触发选项卡按下键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个请求,应该在页面加载时触发Tab键按下键盘事件.我尝试了以下示例,但它不起作用.

I have a request where on page load I should trigger the tab press keyboard event. I tried below example but it does not work.

我的要求是在页面加载时将重点放在父html页面中的元素上,但是我无权访问此父页面的源代码.我的html页面是作为框架的父项的一部分.因此,在加载页面(父级已加载)时,我想触发Tab键按下事件以将焦点设置在父级元素上.

My requirement is to set focus on a element in parent html page on page load, but I do not have access to the source code of this parent page. My html page is part of the parent as a frame. So on load of my page (parent is already loaded) I want to trigger the tab press key event to set the focus on the element from parent.

注意:此元素是父页面中的第一个元素,因此,在第一个选项卡上,焦点转移到了此元素.我无法编辑父页面

Note : This element is the first element in the parent page and hence on first tab the focus shifts to this. I cannot edit the parent page

<!DOCTYPE html>
<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
         	$("#input1").focus();
             var press = jQuery.Event("keypress");
             press.which = 9;
             $("#input1").trigger(press);
         });
         
         
      </script>
   </head>
   <body>
      <input type="text" name="myInput1" id="input1" />
      <input type="text" name="myInput2" id="input2" />
      <input type="text" name="myInput3" id="input3" />
   </body>
</html>

推荐答案

$("1")无效.您必须按类$(".1")或ID $("#1")来获取元素.您的情况是$("#1").

This $("1") won't work. You have to grab the element by class $(".1") or id $("#1"). In your case it would be $("#1").

这篇关于页面加载时触发选项卡按下键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆