jquery Bind a iframe (tinymce)

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:35:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!