【问题标题】:Find hosting tag of iFrame from within the iframe从 iframe 中查找 iFrame 的托管标签
【发布时间】:2021-05-24 14:23:48
【问题描述】:

我有以下html代码:

<div id="container">
   <h1> here is the iframe </h1>
   <iframe src="...."></iframe>
</div>

我需要能够从 iFrame 中获取 iframe 所在的 div (id="container") 的 id。这可能吗?

谢谢!

【问题讨论】:

  • 查看this question
  • 谢谢,我没有看到任何对我有用的东西。
  • 简短的回答是否定的。正如该问题的答案中所解释的那样,长答案是,如果它们是同一来源,您应该能够访问 parent 属性并从那里继续。

标签: javascript iframe


【解决方案1】:

我最后所做的是使框架 id 成为其容器 id 的函数。 例如:

<div id="container1">
    <iframe id="container1_iframe" src="..."></iframe>
</div>

然后,从 iFrame 中我得到了这样的 iframe id:window.frameElement.id

我从它的值中扣除了容器 id。

【讨论】:

  • 应该不需要通过ID绕道,如果你可以通过window.frameElement访问iframe元素,那么你也应该可以通过window.frameElement.parentNode直接访问其父元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
相关资源
最近更新 更多