【问题标题】:Get parent element of iframe if iframe src is cross domain如果 iframe src 是跨域,则获取 iframe 的父元素
【发布时间】:2015-06-23 07:10:49
【问题描述】:

我有一个简单的 html 网页,猜测主页域是 ma​​inDomain.com 。 这个网页包含<iframe id="a1" src="http://crossdomain.com/page1.htm">标签,这个<iframe>标签src是跨域的。 <iframe id="a1" src="http://crossdomain.com/page1.htm"> 包含另外一个 <iframe> 标记,即 src <iframe id="a1" src="http://mainDomain.com/page2.html">

我想获取<iframe id="a1" src="http://crossdomain.com/page1.htm"> 的内容,使用主页或<iframe id="frame2">。 请帮助我,我该怎么做?

<!doctype>
<html>
<head></head>

<body>
<h1>hello world</h1>
<iframe src="http://crossdomain.com/page1.htm"  id="frame1">         
     <h2 id="hader1" > hey dad </h2>            
        <iframe src="http://mainDomain.com/page2.html" id="frame2">
          <p>this is second frame</p>
        </iframe>            
</iframe>
</body>
</html>

【问题讨论】:

  • 不能跨域访问,除非可以control the other domain
  • 我想以任何方式做到这一点,请给我一个建议
  • You simply can't 与 JS。
  • 使用服务器和类似 cURL 的东西下载服务器上的内容。大概就是这样,除非您编写浏览器扩展程序,否则浏览器不会授予您在浏览器中的访问权限。

标签: javascript html dom iframe


【解决方案1】:

如果我正确理解您的问题,您希望将内容从&lt;iframe id="frame2"&gt; 获取到&lt;iframe id="frame1"&gt;

我认为 JQuery 是获取内容作为跨域访问的有用方法。

<script>
$('#frame1').load(function(){
    $('#frame').contents().find('#hader1').text();
});
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2014-11-28
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多