如何在iframe之外获取元素

时间:2018-09-23 08:53:40

标签: javascript html iframe getelementbyid

我有下面的代码。 inviter嵌入在myscript.js中。 https://example.com/iframe3.html嵌入在iframe3.html中。我的问题是,如何更改homepage.html,以使其能够在iframe之外获取内容。本质上,我试图抢myscript.jsid="name"


myscript.js

id="age-i"

homepage.html

    var ifr = document.createElement("iframe");
    var theage = document.getElementById("age-i").innerHTML;
    ifr.src = "https://example.com/get.php?age-i=" + theage;
    ifr.style.visibility = "hidden";
    document.body.appendChild(ifr);

iframe3.html

<td width="340" align="right">
  <div id="userarea">
    <div>
      <div id="userarea-line1">
        <a href="#"><img src="/common/img/icons/home.png" width="14" height="14" /></a> <a id="name" href="#"><span class='notranslate'>John</span></a> | <a href="#">Account</a> | <a href="#" style="margin-right:5px;">Smith</a> </div>
      <div id="userarea-line2"> <img width="10" height="11" src="/common/skin/base/icons/icn_age.gif" />&nbsp; <span class="age-format"> <a href="#"><strong id="age-i"><span class='notranslate'>26</span></strong> age</a>
      </span> <img width="10" height="11" align="top" src="/common/skin/base/icons/icn_age.gif" /> <span class="age-format" style="margin-right:5px;"> <span id="month-i"> <span class='notranslate'>03</span> </span> month</span>
      </div>
    </div>
  </div>
</td>
<iframe id="myframe_content" frameborder="0" src="https://example.com/iframe3.html" name="myframe_content"></iframe>

1 个答案:

答案 0 :(得分:0)

我相信现有的stackoverflow帖子:accessing elements of one iframe from another iframe应该回答您的问题。

谢谢,
赤丹