【问题标题】:iframe: How can we change iframe src from inside iframe itself?iframe:我们如何从 iframe 本身内部更改 iframe src?
【发布时间】:2013-12-26 16:40:35
【问题描述】:

我知道有人提出了与此类似的问题,但我仍然没有得到正确的概念。我目前正在通过以下方式通过 javascript 加载 iframe:

jQuery('.out-div-login').html("<iframe id='parent' frameBorder='0' width='320' height='500' src='"+_url+"'></iframe>");  

哪个工作正常。 (它的跨域)。在给定的 iframe 中加载我的应用程序后,我想回到 .out-div-login 将 iframe 加载到父 html 中的原始状态。
从 iframe 外部,我可以通过使用其 id 属性访问 iframe 来做到这一点,但不能从内部访问。有什么办法可以通过再次提供它的 src 来重新加载 iframe 吗?还是通过上面的代码,但从 iframe 内部?谢谢。
更新
到目前为止,我已经尝试了以下代码但没有任何成功:

var ifr = $('iframe[id="parent"]');  

来自iframe 内的 js 文件之一,ID 为 parent。当我执行console.log(ifr) 时,它会在萤火虫中提供类似Object[] 而不是&lt;iframe id="parent"&gt;

【问题讨论】:

  • @Sebi2020 感谢您的回复,但给定链接中的代码仅在我们尝试从 iframe 外部执行时才有效。!!我已经尝试过了,我什至无法从当前的 iframe 中获取 iframe 引用

标签: javascript jquery iframe


【解决方案1】:

您应该在 iframe 中使用 location.href。那应该行得通。将 iframe 中的 location.href 设置为您的新位置。

【讨论】:

  • 所以location.href 定位到当前文档!!因为这个 iframe 是当前文档。很好,谢谢:)
【解决方案2】:

url查询字符串(?xxx)也应该保留在新的url中,所以整个解决方案应该是:

location.href = "new_link_url" + location.search;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 2012-10-07
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多