简体   繁体   English

无法从iframe访问父元素

[英]Can't access parent elements from iframe

I do not get it. 我不明白。 I thought that I didi it, and now I cannot do it. 我以为我做了它,现在我不能这样做。 Simple html with an iframe. 带iframe的简单html。 Indide the iframe is another html. iframe是另一个HTML。 I want to change an image from the main iframe when you access the iframe. 我想在您访问iframe时更改主iframe中的图片。 HTML from the main html: 来自主html的HTML:

Main 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 的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>

It is supposed to change the picture when you click on the link. 当您单击链接时,它应该更改图片。 But the ALERT is not showing... I've tried different ways and I'm really lost. 但ALERT没有显示......我尝试过不同的方式而且我真的输了。 Is so difficult to achieve this simple change? 难以实现这种简单的改变吗​​?

You can refer to this answer as it does what you need with jquery. 您可以参考这个答案,因为它可以满足您对jquery的需求。

how to access iFrame parent page using jquery? 如何使用jquery访问iFrame父页面?

From the above example selecting an element from an iframe in the parent document use 从上面的示例中选择父文档中iframe的元素使用

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

Checked your page (chrome 40) and got 检查了你的页面(chrome 40)并得到了

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

so I checked in a server (local express) and works fine. 所以我检查了服务器(本地快递)并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM