Javascript:iFrame中的事件 [英] Javascript: Event in iFrame

查看:111
本文介绍了Javascript:iFrame中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个 WYISWYG 编辑器,其中 iframe designMode =' '



问题是我不能在Firefox和Opera(IE未经测试)的iframe上使用任何事件,例如我想追踪onkeyup事件:

  document.getElementById(myFrame)。onkeyup = function(){
doSomething ...
}

但在父窗口中不起作用。 / p>

我也在iframe中尝试过:

  top.frames [0] .onkeyup = function(){
doSomething ...
}

和所有类似的东西:

  top.document.frames [0] .onkeyup 
顶部。框架[myFrame]。onkeyup
top.frames [0] .document.onkeyup

但是没有人想要工作,所以最终证明,即使 window.onclick 不起作用,所以现在我有点困惑...

这是什么解决方案?



编辑



问题似乎是与 document.designMode =on在iframe中

解决方案

我建议在iframe的文档中捕获事件,而在Firefox中至少需要使用 addEventListener()而不是 onkeyup 。以下内容将适用于所有主流浏览器:

  var iframe = document.getElementById(myFrame); 
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

函数handleIframeKeyUp(evt){
alert(Key up!);
}

if(typeof iframeDoc.addEventListener!=undefined){
iframeDoc.addEventListener(keyup,handleIframeKeyUp,false);
} else if(typeof iframeDoc.attachEvent!=undefined){
iframeDoc.attachEvent(onkeyup,handleIframeKeyUp);
}


I'm building a WYISWYG editor with an iframe with designMode = 'on'.

The problem is that I can't use any event on the iframe in Firefox and Opera (IE untested), for example I would like to track the onkeyup event:

document.getElementById("myFrame").onkeyup = function(){
    doSomething...
}

But doesn't works in the parent window.

I tried in the iframe too with this:

top.frames[0].onkeyup = function(){
        doSomething...
}

and all kind of stuff like these:

top.document.frames[0].onkeyup
top.frames["myFrame"].onkeyup
top.frames[0].document.onkeyup

But none of them wants to work so at the end turned out that even window.onclick doesn't works, so now I'm a bit confused...

What's the solution for this?

EDIT

The problem seems to be with document.designMode = "on" in the iframe

解决方案

I would suggest catching the event on the iframe's Document, and in Firefox at least you need to do this using addEventListener() rather than onkeyup. The following will work in all major browsers:

var iframe = document.getElementById("myFrame");
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

function handleIframeKeyUp(evt) {
    alert("Key up!");
}

if (typeof iframeDoc.addEventListener != "undefined") {
    iframeDoc.addEventListener("keyup", handleIframeKeyUp, false);
} else if (typeof iframeDoc.attachEvent != "undefined") {
    iframeDoc.attachEvent("onkeyup", handleIframeKeyUp);
}

这篇关于Javascript:iFrame中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆