我有一个拥有自己应用程序的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/