将焦点设置为 iframe 内容 [英] Setting focus to iframe contents

查看:49
本文介绍了将焦点设置为 iframe 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 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屋!

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