将焦点设置为iframe内容 [英] Setting focus to iframe contents
问题描述
我有一个包含 document.onkeydown
事件处理程序的页面,我将其加载到另一个页面的iframe中。我必须点击iframe里面的内容页面才能开始听。
I have a page with a document.onkeydown
event handler, and I'm loading it inside an iframe in another page. I have to click inside the iframe to get the content page to start "listening".
有什么办法可以在外部页面中使用JavaScript来将焦点设置到内部页面,这样我就不必在iframe内单击了。
Is there some way I can use JavaScript in the outer page to set the focus to the inner page so I don't have to click inside the iframe?
编辑:回复评论:
上下文是主窗口是一个灯箱式系统,的图片,它显示iframe,每个iframe是一个与keydown / mousemove处理程序的互动页面。这些处理程序不要点火,直到我点击iframe显示灯箱的东西。
The context is the main window is a light-box-like system, except instead of pictures, it shows iframes, and each iframe is an interactive page with keydown/mousemove handlers. these handlers don't fire until I click in the iframe after showing the light-box-thing.
我实际上并没有在传统意义上寻求setFocus与iframe contentDocument中的启用事件处理程序一样多
I'm not actually looking to "setFocus" in the traditional sense as much as "enable event handlers on the iframe contentDocument"
推荐答案
我和jQuery Thickbox有一个类似的问题样式对话框小部件)。我修复我的问题的方式如下:
I had a similar problem with the jQuery Thickbox (a lightbox-style dialog widget). The way I fixed my problem is as follows:
function setFocusThickboxIframe() {
var iframe = $("#TB_iframeContent")[0];
iframe.contentWindow.focus();
}
$(document).ready(function(){
$("#id_cmd_open").click(function(){
/*
run thickbox code here to open lightbox,
like tb_show("google this!", "http://www.google.com");
*/
setTimeout(setFocusThickboxIframe, 100);
return false;
});
});
如果没有setTimeout(),代码似乎不起作用。根据我的测试,它适用于Firefox3.5,Safari4,Chrome4,IE7和IE6。
The code doesn't seem to work without the setTimeout(). Based on my testing, it works in Firefox3.5, Safari4, Chrome4, IE7 and IE6.
这篇关于将焦点设置为iframe内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!