【问题标题】:Can't access parent elements from iframe无法从 iframe 访问父元素
【发布时间】:2015-02-24 22:16:01
【问题描述】:

我不明白。我以为我做了,现在我做不到。 带有 iframe 的简单 html。在 iframe 中是另一个 html。当您访问 iframe 时,我想从主 iframe 更改图像。 来自主 html 的 HTML:

主要的html:

<!DOCTYPE HTML>
 <html lang="en">
    <head></head>
    <body>
        <img id="logo" src="../img/fpdfilms-logo-home.png" />
        <iframe id="fondo" name="main" src="test.html"></iframe>
    </body>
</html>

test.html

<!DOCTYPE HTML>
 <html lang="en">
    <head></head>
    <body>
        <a href="#" onclick="change();">Change Picture</p>
        <script src="../js/jquery-1.11.2.min.js"></script>
        <script type="text/javascript">
            function change()
            {
                var $logo= $('#logo', window.parent.document);
                alert("Does it work?");
                $logo.attr("src","../img/fpdfilms-logo-director.png");
            }
        </script>
    </body>
</html>

单击链接时应该会更改图片。但是警报没有显示...我尝试了不同的方法,但我真的迷路了。这么简单的改变就这么难实现吗?

【问题讨论】:

标签: javascript iframe


【解决方案1】:

检查了你的页面(chrome 40)并得到了

SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

所以我签入了服务器(本地快递)并且工作正常。

【讨论】:

  • ...谢谢,但您能指导我如何解决这个问题吗?
  • 我想通了。我正在检查本地PC中的文件。服务器不一样!!!它有效!!!!
【解决方案2】:

您可以参考这个答案,因为它可以满足您对 jquery 的需求。

how to access iFrame parent page using jquery?

从上面的示例中,从父文档的 iframe 中选择一个元素,使用

var $parentElement= $('#parentElement', window.parent.document);

【讨论】:

  • 我已经编辑了 html 以成为最简单的代码。它不起作用......还有其他想法吗?
猜你喜欢
  • 2013-08-05
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
相关资源
最近更新 更多