【问题标题】:passing event object in function inside iframe IE8在 iframe IE8 内的函数中传递事件对象
【发布时间】:2013-03-08 09:35:13
【问题描述】:

我正在 iframe 中动态加载 .htm 文件(.htm 在我的域中),如下所示:

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 传递事件对象?

提前致谢!

【问题讨论】:

    标签: javascript internet-explorer


    【解决方案1】:

    首先,以这种方式分配您的 ifra 字符串:

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

    否则无法运行。

    然后,尝试用var定义变量,这样可以防止它的内容变成undefined

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2010-11-19
      相关资源
      最近更新 更多