gpt4 book ai didi

javascript - 在 iframe 中获取子对象的实例

转载 作者:数据小太阳 更新时间:2023-10-29 04:10:44 26 4
gpt4 key购买 nike

我需要自动化一个 Web 表单,该表单使用 iframe 嵌入子“表单”,可以将其视为单独的 html 页面。

因此,您会得到一个父文档,其中包含他们称之为 iframe 的 View 字段。在 iframe 中是“子”嵌入式网页。

一个简单的例子:

我已经删除了很多 html 以将其缩小到合理的大小

<html>
<body>
<div id="123" arid=123 artype="View" ardbn="vfMyChild"
class="arfid123 ardbnvfMyChild"
STYLE="top:49 left:75 width:1038 height:322" arvfframe="&lt;iframe style=&quot;top:0 left:0 width:1038; height:322&quot name=&quot;VF123&quot; title=&quot;View Field&quot; src=&quot;javascript:&amp;quot&#59;&amp;lt&#59;HTML&amp;gt&#59;&amp;lt&#59;/HTML&amp;gt&#59;&amp;quot&#59;&quot; &gt;
&lt;/iframe&gt;">
</div>

</body>
</html>

那么嵌入的子 html 可以像这样简单:

<html>
<body>
<div id="321" arid=321 artype="Char" ardbn="txtRegister">
<label id="label321" class="label f6">Register:</label>
<textarea id="arid_321" cols="20" maxlen=255 rows=1></textarea>
</div>
</body>
</html>

STYLE= 中包含的 html 看起来有点奇怪 - 不确定为什么会这样。但我可以看出它有点像 iframe。

我有一个顶级文档对象的实例和一个 id=123 的 div 实例。我需要自动化子项中的 textarea 对象。我尝试了以下方法,但没有用。

var viewfields;  //is a ref to the div with id=123

if(viewfields) {
window.alert("viewfields.length=" + viewfields.length); //prints len=1 as expected
// line below get Caught exception: Unable to get value of the
// property 'document': object is null or undefined
var innerDoc = viewfields[0].contentDocument || viewfields[0].contentWindow.document;
if(innerDoc) {
window.alert("Wohoo we have an innerDoc");
}
} else
window.alert("no view fields found");

我正在使用 IE9 对此进行测试。

我能否使用此 html 获取内部网页的实例?如果是,怎么办?

编辑

如果有帮助,这里是相关 div 的实际、未经编辑的 html。

<div id="WIN_0_536870915" arid=536870915 artype="View" ardbn="vfCubaChildren"  class="arfid536870915 ardbnvfCubaChildren" STYLE="top:49&#59; left:75&#59; width:1038&#59;  height:322&#59;z-index:1001&#59;background-color:transparent&#59;" arvfframe="&lt;iframe  style=&quot;top:0&amp;#59&#59; left:0&amp;#59&#59; width:1038&amp;#59&#59;  height:322&amp;#59&#59;background-color: transparent&amp;#59&#59;&quot;  name=&quot;VF536870915&quot; frameborder=1 scrolling=&quot;auto&quot;  allowtransparency=&quot;true&quot; title=&quot;View Field&quot;  src=&quot;javascript:&amp;quot&#59;&amp;lt&#59;HTML&amp;gt&#59;&amp;lt&#59;/HTML&amp;gt&#59    ;&amp;quot&#59;&quot; onload=&quot;DVFol&amp;#40&#59;&amp;#41&#59;&quot;&gt;
&lt;/iframe&gt;">
</div>

那个 div 包含子窗体。子表单就像另一个 html 网页。它具有标准的 html 输入字段和文本区域。我需要将文本发布到 child (来自 parent )的文本区域。但问题是我还不能从父对象访问子 html 对象。

最佳答案

如果您正在尝试从查看 iframe 的父级获取数据,那么这就是您要查找的内容。请记住,这仅在 iframe 源是来自同一域的页面时才有效,否则您将无权访问其内容。

http://jsfiddle.net/wao20/Ja54y/19/

首先确保您的 iframe 已加载,然后您可以调用它来访问您的 iframe dom。

$('#iframe_id_123').contents().find('#element_inside_your_iframe');

更新:

如果你想要一个纯 js 解决方案,你可以试试这个:

var f = document.getElementById('f123');
if (f != undefined){
var doc = f.contentDocument || f.contentWindow.document;
var txt = doc.getElementById('secret');
alert(txt.value);
}
else {
alert('No iframe, no secret!');
}

http://jsfiddle.net/wao20/Ja54y/34/

关于javascript - 在 iframe 中获取子对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865323/

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