6

My iframe content responds to key press, is there a way to send key press into the iframe from the page which contains the iframe?

By the way, the usual way to simulate key press does not work in the scenario:

var ev = $.Event('keypress');
ev.which = 68;
ev.ctrlKey = true;
$('#frame').trigger(ev);
4

1 回答 1

3

This is an old question, but I hope this answer is helpful to someone.

There is a new feature in HTML5, window.postMessage, which allows for communication between an iframe and the hosting window. There's some good documentation here.

As usual, you can expect some inconsistencies across browsers: in my experience, Firefox accepts any object as the message, but IE will only accept a string.

There have been some developments to make this feature easy to use in different browsers, including polyfills to make it work in older browsers.

The one that I ended up using was Ben Alman's jquery plugin.

Enjoy!

于 2013-08-25T09:59:58.357 回答