使用 JQuery 向 iframe 添加事件处理程序 [英] Adding event handler to an iframe using JQuery
本文介绍了使用 JQuery 向 iframe 添加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为 iframe 分配一个 keydown 事件处理程序.类似于纯JS的东西:
I want to assign a keydown event handler to an iframe. Something similar to the pure JS:
document.getElementById('iframe_id').contentWindow.addEventListener('keydown',funcName, true);
我试过了:
$(document.getElementById('iframe_id').contentWindow).keydown( function() {
// my func
});
但它不起作用..请帮忙!
But it does not work.. Please help!
推荐答案
contentWindow
是 iframe
的 window
对象.你想要 iframe
的 document
代替:
contentWindow
is the iframe
's window
object. You want the iframe
's document
instead:
$(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
// my func
});
请注意,我不确定 jQuery 如何对来自其他窗口/框架的元素做出反应.
Note that I am not sure how jQuery reacts to elements from other windows/frames.
这篇关于使用 JQuery 向 iframe 添加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文