【问题标题】:Child iframe doesn't find parent子 iframe 找不到父级
【发布时间】:2014-08-08 12:43:58
【问题描述】:

我已经在 stackoverflow 上看到了很多关于这个主题的问题,但没有任何帮助。

我有一个简单的情况:

父页面:

<!DOCTYPE html>
<html>
<head>
    <title>title</title>
</head>
<body>
    <script type="text/javascript">
        var global = 'I am global variable';
    </script>
    <iframe name="main" src="main.php"></iframe>
</body>
</html>

所以...子 iframe 页面

<!DOCTYPE html>
<html>
<head>
    <title>iframe window</title>
</head>
<body>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    <script type="text/javascript">
        var local = 'i am local variable';
    </script>
</body>
</html>

你看,没什么难的。

如果我在 index.php(父级)中找到子 iframe - 它返回子级的窗口对象(在控制台中),一切都很好。

但是,如果我想使用任何下一个命令找到父级:

window.frames.parent

window.frames.top

或寻找变量

window.frames.parent.global

它返回同一页面的对象窗口,但不返回父级。

它在 WAMP 中工作,具有简单的结构,包含 index.php 和 main.php。有什么想法吗?

【问题讨论】:

    标签: javascript html iframe


    【解决方案1】:

    试试这个:

    alert(parent.global); // inside main.php(child) file
    

    【讨论】:

    • 对不起,伙计...但它不起作用,这就是为什么这是一个奇怪的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2016-03-15
    • 1970-01-01
    相关资源
    最近更新 更多