2013-03-08 56 views
-1

我动态加载一个.htm文件的iframe内(与.htm是在我的域)这样的:通过事件对象在函数内部的iframe IE8

el.innerHTML = "<iframe id='frmBook' onload='on_load()' src='blabla.htm'"; 

我ON_LOAD是这样的:

function on_load() { 

    document.getElementById("frmBook").contentWindow.document.body.ondblclick = function(event) { 
     var oTextRange; 
     if (!document.selection) { 
      oTextRange = window.getSelection(); 
      if (oTextRange.rangeCount > 0) oTextRange.collapseToStart(); 
     } 
     getWord(event); 
    } 

    document.getElementById("frmBook").contentWindow.document.body.oncontextmenu = function(event) { 
     showContextMenu(event); 
     return false; 
    } 
} 

现在我需要传递事件对象,因为它在getWord()和showContextMenu()中都使用。它用于getWord()获取e.target.id(或e.srcElement),并在showContextMenu()中使用e.page.X.麻烦的是,IE8无法识别(未定义)事件对象,因此它无法通过。有没有办法通过IE8的事件对象?

在此先感谢!

回答

-1

首先,这种方式分配您ifram字符串:

el.innerHTML = '<iframe id="frmBook" onload="on_load()" src="blabla.htm"></iframe>'; 

否则无法运行。

然后,尝试与VAR定义变量,这样可以防止它的内容变得不确定

function on_load() { 

    document.getElementById("frmBook").contentWindow.document.body.ondblclick = function(event) { 
     var e = event; 
     var oTextRange; 
     if (!document.selection) { 
      oTextRange = window.getSelection(); 
      if (oTextRange.rangeCount > 0) oTextRange.collapseToStart(); 
     } 
     getWord(e); 
    } 

    document.getElementById("frmBook").contentWindow.document.body.oncontextmenu = function(event) { 
     var e = event; 
     showContextMenu(e); 
     return false; 
    } 
} 
相关问题