简体   繁体   English

页面加载时触发选项卡按下键事件

[英]trigger tab press key event on page load

I have a request where on page load I should trigger the tab press keyboard event. 我有一个请求,应该在页面加载时触发Tab键键盘事件。 I tried below example but it does not work. 我尝试下面的示例,但它不起作用。

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. 我的要求是在页面加载时将重点放在父html页面中的元素上,但是我无权访问此父页面的源代码。 My html page is part of the parent as a frame. 我的html页面是作为框架的父项的一部分。 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. 因此,在加载页面(父级已加载)时,我想触发Tab键按下事件以将焦点设置为父级元素。

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> 

This $("1") won't work. $("1")无效。 You have to grab the element by class $(".1") or id $("#1") . 您必须按类$(".1")或id $("#1")来获取元素。 In your case it would be $("#1") . 在您的情况下,它将是$("#1")

To find in the parent of the iFrame use: 要在iFrame的父项中查找,请使用:

$('#parentPrice', window.parent.document).focus();

The second parameter for the $() wrapper is the context in which to search. $()包装器的第二个参数是要在其中搜索的上下文。 This defaults to document. 默认为文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM