问题
I can bind eventsin the current window, but I'm using TinyMCE which creates an iframe, and I want to be able to set a keybind to the iframe window to capture a control-S
I have:
$(window.child).keydown(function(e) {
if(!args) args=[]; // IE barks when args is null
if(e.keyCode == key.charCodeAt(0) && e.metaKey) {
callback.apply(this, args);
return false;
}
});
But that isn't working. Ideas?
Thanks
回答1:
It's at first a problem of timing, when to access the iframe.
If you bind it somewhere inside the parent document, it may be that the document inside the iframe isn't loaded yet. So I would suggest to assign it directly to the onload-event of the iframe, to be sure that it is accessable. Furthermore it depends on the browser, how you'll get a to pointer the document:
<iframe onload="fx(this)" src="some.html" ></iframe>
<script type='text/javascript'>
function fx(win)
{
var doc=win.contentDocument||win.contentWindow.document;
$(doc).keypress(
function(event)
{
alert(event.charCode||event.keyCode);
});
}
</script>
回答2:
$( document.iframe1.contentWindow.document).keydown(...);
iframe1 is an iframe
来源:https://stackoverflow.com/questions/4288222/jquery-bind-a-iframe-tinymce