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

9 javascript iframe

我有2页 - 父母和孩子.

在父页面中,我有一个iframe,其值我想在子页面的javascript中获取.有什么办法吗?....请建议.

小智 8

实际上只iframeElement需要.

从子文档内部:

 var iframe = frameElement;
Run Code Online (Sandbox Code Playgroud)

你完成了


Vin*_*lds 7

假设您的页面和框架结构如下

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

然后

  1. iframe和其他页面的src是相同的
  2. iframe的名称是'iFrame',

您可以使用以下来自子代的JavaScript语句访问iframe中名为'iFrameElement'的元素:

parent.frames['iFrame'].document.getElementById('iFrameElement').value;
Run Code Online (Sandbox Code Playgroud)

或者只是包含iframe的父项中的以下内容

frames['iFrame'].document.getElementById('iFrameElement').value;
Run Code Online (Sandbox Code Playgroud)

由于帧名在运行时是不确定的,您可以恢复使用window.frames数组中的帧编号,如下所示(来自子)

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

或来自父母

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


Kri*_*son 1

您可以使用

window.parent.getElementById('YOUR ID').value();
Run Code Online (Sandbox Code Playgroud)

从父元素中获取 id 为“YOUR ID”的输入元素的值。