gpt4 book ai didi

javascript - 在 JavaScript 中访问 iframe 元素

转载 作者:行者123 更新时间:2023-12-03 00:32:29 25 4
gpt4 key购买 nike

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

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

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

最佳答案

如果您的 iframe 与父页面位于同一域中,您可以使用 window.frames 集合访问元素。

// 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 不在同一域中,出于安全原因,浏览器应阻止此类访问。

关于javascript - 在 JavaScript 中访问 iframe 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1451208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com