9

我需要自动化一个使用 iframe 嵌入子“表单”的 web 表单,这些表单可以被视为单独的 html 页面。

因此,您会得到一个父文档,其中包含他们称为 iframe 的视图字段。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 输入字段和 textarea。我需要将文本发布到孩子(来自父母)的文本区域。但问题是我还不能从父级访问子 html 对象。

4

3 回答 3

8

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

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

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

$('#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/

于 2013-04-17T23:08:26.423 回答
2

iframe 的内容可能未加载。您可以尝试检查 iframe 是否已加载,如果已加载,则尝试访问其文档元素。

例如,访问 iframe 的文档元素的代码被拆分为在AfterFrameLoads()iframe 的 onload 事件期间调用的函数。

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

if(viewfields) {
    window.alert("viewfields.length=" +  viewfields.length);  //prints len=1 as expected

    // Check if iframe loading is complete, attach rest of code to onload event 
    if (viewfields[0].addEventListener){ // for Mozilla, FF, Chrome, etc...
        viewfields[0].addEventListener("onload",AfterFrameLoads());
    }
    else if (viewfields[0].attachEvent){ // for IE and Opera
        viewfields[0].attachEvent("onload",AfterFrameLoads());
    }
 }

// Attempt to access document after iframe loads.

function AfterFrameLoads(){   
     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");
    }
}
于 2013-04-14T04:51:59.250 回答
0

var 视场;//是对 id=123 的 div 的引用`

if(viewfields) { window.alert("viewfields.length=" + viewfields.length); //按预期打印 len=1

// Check if iframe loading is complete, attach rest of code to onload event 
if (viewfields[0].addEventListener){ // for Mozilla, FF, Chrome, etc...
    viewfields[0].addEventListener("onload",AfterFrameLoads());
}
else if (viewfields[0].attachEvent){ // for IE and Opera
    viewfields[0].attachEvent("onload",AfterFrameLoads());
}

}

// 尝试在 iframe 加载后访问文档。

函数 AfterFrameLoads(){
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");
}

}

于 2021-05-04T10:06:11.413 回答