访问JavaScript中的iframe元素 [英] Access iframe elements in JavaScript

查看:77
本文介绍了访问JavaScript中的iframe元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,其中iframe中有一个textarea。我需要使用JavaScript从其子页面读取此textarea的值。

I have a webpage where there is a textarea within an iframe. I need to read the value of this textarea from its child page using JavaScript.

目前使用 window.parent.getelementbyID()。value 在JavaScript中,我能够获取父页面中除iframe中的textarea之外的所有控件的值。

Presently by using window.parent.getelementbyID().value in the JavaScript, I am able to fetch values of all controls in the parent page except the textarea within the iframe.

任何人都可以给我任何指针来解决这个问题?

Can anyone please give me any pointers to resolve this issue?

推荐答案

如果您的iframe与您的父页面位于同一个域中,您可以使用 document.frames collection。

If your iframe is in the same domain as your parent page you can access the elements using document.frames collection.

// replace myIFrame with your iFrame id
// replace myIFrameElemId with your iFrame's element id
// you can work on document.frames['myIFrame'].document like you are working on
// normal document object in JS
window.frames['myIFrame'].document.getElementById('myIFrameElemId')

如果您的iframe不在同一个域中,浏览器应阻止此类访问出于安全原因。

If your iframe is not in the same domain the browser should prevent such access for security reasons.

这篇关于访问JavaScript中的iframe元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆