【问题标题】:How do i run a js function from an iframe in the main frame?如何从主框架中的 iframe 运行 js 函数?
【发布时间】:2011-03-24 20:35:06
【问题描述】:

在我的 iframe 中,我有一个功能:

function CheckDebug(){
        if($('#debug').length > 0){
            debugConsole.debugOn = true;
            debugConsole.IP = "<?=$_SERVER['REMOTE_ADDR']?>";
            debugConsole.log('START DEBUG MODE');
        }
    }

如何在主框架中运行该函数以影响 iframe 的 debugConsole 对象?

【问题讨论】:

    标签: php javascript jquery iframe


    【解决方案1】:

    要访问父 iframe 使用window.parent,您可以调用:

    window.parent
    

    所以你会这样做:

    window.parent.debugConsole(..);
    

    举个例子。

    要让父级访问 iframe,您可以执行以下操作:

    document.getElementById('frameId').contentWindow.funcThatLivesInIframe();
    

    其中 iframe 的 id 属性值为“frameId”。

    【讨论】:

    • 你的第二部分成功了!非常感谢!我不能再接受 9 分钟,哈哈
    【解决方案2】:

    只需访问“顶部”上下文:

    function CheckDebug(){
        if (top.$('#debug').length > 0){
            top.debugConsole.debugOn = true;
            top.debugConsole.IP = "<?=$_SERVER['REMOTE_ADDR']?>";
            top.debugConsole.log('START DEBUG MODE');
        }
    }
    

    现在是最顶层的窗口。如果只是想上一层,可以看“window.parent”。

    【讨论】:

    • 我不知道你所说的“操纵 iframe”是什么意思。你到底想对它做什么?父/子关系可以通过多种方式实现,您还没有发布那么多代码来明确您的页面是做什么的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 2017-11-27
    • 2019-03-31
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多