admin管理员组文章数量:1410682
In my application, I have an iframe and I would like to give it access to some functionality which changes the state of the application outside the iframe. This functionality must be triggered by a keydown
event either inside or outside the iframe.
In the iframe, I have the following code:
window.init = function (api) {
api.bind(document);
};
And the outer application does something like:
iframe.contentWindow.init({
bind: function (element) {
$(element).bind('keydown', function () {
debugger;
});
}
});
With this, init
is called properly, but the keydown
event handler is never called. What am I doing wrong?
In my application, I have an iframe and I would like to give it access to some functionality which changes the state of the application outside the iframe. This functionality must be triggered by a keydown
event either inside or outside the iframe.
In the iframe, I have the following code:
window.init = function (api) {
api.bind(document);
};
And the outer application does something like:
iframe.contentWindow.init({
bind: function (element) {
$(element).bind('keydown', function () {
debugger;
});
}
});
With this, init
is called properly, but the keydown
event handler is never called. What am I doing wrong?
2 Answers
Reset to default 4If you try to bind event in this way:
$(element).bind('keydown', function () {
alert("hello");
});
jquery will try to traverse your element in parent window
, but if you do the following your problem would be solved:
$(iframe.contentWindow.document.body).find(element).bind('keydown', function () {
alert("hello");
});
The problem is that some part of the iframe has already attached a keydown
handler which stops propagation of the event, preventing the event from reaching the document and preventing the handler from being called. Nothing was wrong with the actual binding.
本文标签: javascriptAttach event to iframe element in parent codeStack Overflow
版权声明:本文标题:javascript - Attach event to iframe element in parent code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744912239a2631988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论