在 JavaScript 中访问 iframe 元素 [英] Access iframe elements in JavaScript
问题描述
我有一个网页,其中 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.
目前通过在 JavaScript 中使用 window.parent.getelementbyID().value
,我能够获取父页面中除 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 与您的父页面在同一个域中,您可以使用 window.frames
集合访问元素.
If your iframe is in the same domain as your parent page you can access the elements using window.frames
collection.
// replace myIFrame with your iFrame id
// replace myIFrameElemId with your iFrame's element id
// you can work on window.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屋!