简体   繁体   English

如何从子 javascript 页面访问父 iframe 元素?

[英]How to access parent iframe elements from a child javascript page?

I have 2 pages: Parent and child.我有 2 页:父母和孩子。

In the parent page I have an iframe whose value I want to fetch in the JavaScript of child page.在父页面中,我有一个 iframe,我想在子页面的 JavaScript 中获取其值。 Is there any way?....Please suggest.有什么办法吗?......请建议。

Actually only the frameElement variable is needed.实际上只需要frameElement变量。

from inside child document:从子文档内部:

 var iframe = frameElement;

and you're done.你就完成了。

Assuming that your page and frame structure is as follows假设你的页面和框架结构如下

parent (contains iframe)
   |--> child page (attempts to access iframe in parent)

and that然后

  1. the src of the iframe and other pages is the same iframe和其他页面的src是一样的
  2. the name of the iframe is 'iFrame', iframe 的名称是“iFrame”,

you can access an element named 'iFrameElement' in the iframe using the following JavaScript statement from the child:您可以使用来自子级的以下 JavaScript 语句访问 iframe 中名为“iFrameElement”的元素:

parent.frames['iFrame'].document.getElementById('iFrameElement').value;

or simply the following from the parent containing the iframe或者只是来自包含 iframe 的父级的以下内容

frames['iFrame'].document.getElementById('iFrameElement').value;

Since the frame name is indeterminate at runtime, you could revert to using the frame number in the window.frames array, as follows (from the child)由于框架名称在运行时是不确定的,您可以恢复为使用 window.frames 数组中的框架编号,如下所示(来自子项)

//assuming frames[0] refers to the iframe
parent.window.frames[0].document.getElementById('iFrameElement').value;

or from the parent或来自父母

//assuming frames[0] refers to the iframe
window.frames[0].document.getElementById('iFrameElement').value;

Use利用

parent.getElementById('elementId').value;

to get the element.获取元素。 If you have multiple nested iframes, you can get the root parent by using如果您有多个嵌套 iframe,则可以使用

top.getElementById('elementId').value;

Either will work in your case.两者都适用于您的情况。

You can use您可以使用

window.parent.getElementById('YOUR ID').value();

to get the value from the input element with the id "YOUR ID" from the parent.从父元素的 id 为“YOUR ID”的输入元素中获取值。

尝试为您的 iframe 命名并像这样访问 iframe

window.parent.<iframe_name>.document.getElementById()

您可以通过以下方式从 iframe 内部获取父文档中的 iframe 元素:

var iframe = parent.document.getElementById(window.frameElement.id);

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

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