【发布时间】:2023-03-30 14:36:01
【问题描述】:
背景:
我正在帮助一位有混合媒体幻灯片的老朋友,其中一张幻灯片是嵌入了 lytro 相机图像的 iframe(它是交互式的,您可以在移动设备上单击或点击以更改焦点)。
问题:
我遇到的问题是,当您与 iframe 交互时,它会窃取桌面上的键盘焦点,并阻止箭头键允许您更改幻灯片。
我的尝试:
我对此的主要攻击角度是尝试使用 jquery 设置一个计时器,该计时器定期将焦点设置在父文档上,以从 iframe 中移除焦点并允许正确捕获击键。我注意到,如果我单击 iframe 之外的任何位置,则可以正确使用箭头键。
这是我的 jquery 代码,以及关于我为什么尝试每种方法的 cmets。不幸的是,没有任何效果(我也尝试使用 embed 标签而不是 iframe 标签包含 lytro 图像,结果没有变化)。
<script>
//make sure body maintains focus, so that swipe and arrows still work
function focusit(){
$('#focushere').focus(); // this is a div on the main page that I tried to set focus to
$('body').focus(); // tried moving focus to the body
$('embed').blur(); // tried bluring the embed
$('iframe').blur(); // tried bluring the iframe
$('body').click(); // tried faking a click on the body
$('#focushere').click(); //tried faking a click on a element
$(document).click(); // tried click on document
$(document).focus(); //tried setting focus to document
}
setTimeout(focusit, 100);
</script>
【问题讨论】:
-
我相信浏览器只允许您更改窗口焦点以响应用户事件(例如单击或键盘事件)。这就是为什么您的超时方法不起作用的原因。很遗憾,我不知道最适合您的情况的解决方案是什么。
-
@Thayne 据我所知,将焦点设置在用户事件之外非常有效。
标签: javascript jquery iframe focus embed