我有一个 iframe,它有自己的应用程序。我希望能够将目标聚焦在 iframe 之外,但到目前为止它还没有识别出来。在页面本身上,它可以工作,但是一旦进入 iframe(文本区域),我想从 iframe 中跳出快捷方式。该函数在 iframe 内部工作(至少 console.log 工作)但不是实际的焦点事件。我试着先关注文档,然后让它运行操作,但它没有关注 iframe 之外。我必须在应用程序中拥有该功能,以便它能够识别它。
https://jsfiddle.net/5ufc4koc/
<div class="field">
<a href="#">Testing</a>
<iframe>
<div>Text text text
</div>
</iframe>
</div>
App.outside = function () {
$(document).focus();
$(document).on('keydown', function (e) {
if (e.which === 113) {
$('.field:first a').focus();
console.log('testing');
}
});
};
最佳答案
在 iframe 中放置另一个 jquery/java 脚本——关注“parent.window.document”。
$(document).ready(function(){
var form = parent.window.document.querySelector('.field');
var toBeFocusedOn = parent.window.document.getElementById("#whateverIdOnMainPage");
$( document ).on( 'keydown', function ( e ) {
if ( e.keyCode === 133 ) { // key code '27 is ESC'
toBeFocusedOn.focus();
}
});
});
关于jquery - 焦点在 iframe 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39774693/