Adding event handler to an iframe using JQuery

一个人想着一个人 提交于 2019-11-27 09:06:59

contentWindow is the iframe's window object. You want the iframe's document instead:

$(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
    // my func
});

Note that I am not sure how jQuery reacts to elements from other windows/frames.

Just something to keep in mind: this will never work, as far as I understand, if the iframe content is cross-domain. You'll end up with permissions errors: Permission denied for http://... to get property HTMLDocument.nodeType from http://.... Browsers limit parent child dom permissions to same domain.

Mcbeev

The $ function replaces the need for document.getElementById

$('iframe_id').contentDocument.keydown(function() {
   // logic
});

This worked for me:

$('#iframe_id').contents().keydown(function() {
    // my func
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!