简体   繁体   English

访问父iframe元素的容器(iframe中的iframe)

[英]Access container of parent iframe element (Iframe inside iframe)

I have next structure of elements: 我有元素的下一个结构:

<div class="container">
  <iframe class="outer">
    ..
      <iframe class="inner">
        actual markup with scripts here
      </iframe>
  </iframe>
</div>

using pure javascript, i've meneged to get out of "inner" iframe into outer one: 使用纯JavaScript,我已经提到要摆脱“内部” iframe进入外部iframe:

//this is HTML tag of ".outer" iframe
var parent = window.parent.document.getElementsByTagName('html')[0];

but still i need to get to ".container" element in order to manipulate it. 但我仍然需要使用“.container”元素来操纵它。 Can anyone tell me how to get ".container" element from script inside ".inner" element?? 谁能告诉我如何从“ .inner”元素内的脚本中获取“ .container”元素?

Maybe this: 也许这个:

var outer = window.parent;

var mainWindow = outer.parent;

var container = mainWindow.document.getElementsByClassName('container')[0];

but dont forget that your iframes have to be on the same domain. 但不要忘记你的iframe必须在同一个域上。

I would try 我会尝试

var containerElement = window.frames.top.top.document.getElementsByClassName('container')[0];

But be careful: If you try to break out of your frame and want to access a document with a different domain, ports or protocol, your browser (hopefully) will return an exception like this: 但要小心:如果您试图打破框架并希望访问具有不同域,端口或协议的文档,则您的浏览器(希望如此)将返回如下异常:

Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". 未捕获的SecurityError:阻止具有原点“null”的帧访问具有原点“null”的帧。 Protocols, domains, and ports must match. 协议,域和端口必须匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM