在Iframe Load中读取iframe内容

Tar*_*rik 6 html javascript iframe

我只是想学习如何在iframe的onLoad事件中阅读iframe内容并将其内容写入主页?谢谢..

Roh*_*agh 11

您可以在父页面中使用此jquery函数,它还支持许多其他函数来添加和删除内容:

<script type="text/javascript">
        function myFunction() {
         var $currIFrame = $('#Frame1');
         $currIFrame.contents().find("body #idofElement").val('Value i want to set');
          }
</script>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中:#idofElement是iframe中HTML页面中要设置其值的元素.我希望它可以帮助你......


小智 7

这一天我一直在努力.你如何访问iframe似乎很重要.如果您使用,document.getElementById()您将获得一个没有onload事件的Iframe对象.但是,window.frames[]例如,如果通过数组访问

var iframeWindow = top.frames['iframeID'],
Run Code Online (Sandbox Code Playgroud)

你得到一个窗口对象,它有onload事件.

(即使用frame id属性,但ff使用name属性.所以使用两者并使其相同)

然后你可以分配

iframeWindow.onload=function(){iframeContent=iframeWindow.document.body.innerHTML;};
Run Code Online (Sandbox Code Playgroud)

请注意,由于iframeWindow是一个窗口对象,因此您可以使用窗口语法来访问内容.

  • &lt;script type =“ text / javascript”&gt;函数redisplay(){var content = top.frames ['fetch']。document.body.innerHTML; top.frames ['display']。document.body.innerHTML = content; } &lt;/ script&gt; &lt;/ head&gt; &lt;body&gt; &lt;iframe id =“ display” name =“ display” style =“ width:600px; height:500px; border:solid 1px black;”&gt; &lt;/ iframe&gt; &lt;p&gt; &lt;/ p&gt; &lt;iframe src =“同一域中的页面” id =“ fetch” name =“ fetch” style =“ width:600px; height:500px;” onload =“ redisplay();”&gt; &lt;/ iframe&gt; &lt;/ body&gt; (2认同)