单击到 iframe 时如何隐藏父元素

Posted

技术标签:

【中文标题】单击到 iframe 时如何隐藏父元素【英文标题】:How to hide a parent element when click to iframe 【发布时间】:2015-10-05 08:44:58 【问题描述】:

为什么我在不同域中时无法将事件从 iframe 传递给父级

window.onload = function () 
    myFrame = document.getElementById('test2');
    myFrame.contentDocument.addEventListener('click', function()
    
        $("test1").hide();
    );
;
<div id="test1" style="width:150px;height:150px;position:relative;">
    <iframe id="test2" src="http://www.w3schools.com"></iframe>
</div>

我想要这样的东西:http://jsfiddle.net/nqTnz/15/

我尝试使用不同域的相同代码但不起作用:http://jsfiddle.net/z6tco5rg/

【问题讨论】:

对于security reason 同源策略限制阻止它 capture click on div surrounding an iframe 的可能副本 【参考方案1】:

不确定是否相关,但请阅读此链接:

http://www.codeproject.com/Tips/585663/Communication-with-Cross-Domain-IFrame-A-Cross-Bro

【讨论】:

以上是关于单击到 iframe 时如何隐藏父元素的主要内容,如果未能解决你的问题,请参考以下文章