2012-01-16 60 views
1

我有一个页面如何在另一个iframe中访问iframe内容?

<div id="editForm_container"> 
<form id="imageForm" name="imageForm" enctype="multipart/form-data" target="hiddenFrame" method="post" action="test.php">     

    <label><strong>File Name</strong></label> 
    <input type="file" name="fileupload" id="fileupload" /><br /> 
    <input type="submit" name="submit_image" id="submit_image" /> 

</form> 
<iframe id="hiddenFrame" name="hiddenFrame"></iframe> 

和我加载这个页面位于另一个页面的另一个iframe和我写这在我的JS文件

$("#imageFrame").load(function(){ 

this.imageFramecontent = $("#imageFrame").contents(); 
this.imageFramefields.fileField = this.imageFramecontent.find(":input[ name = 'fileupload' ]"); 
this.imageFrameform = this.imageFramecontent.find("form#imageForm"); 
this.hiddenFrame = this.imageFramecontent.find("iframe#hiddenFrame"); 

this.imageFramefields.fileField.change(function(){ 

    self.imageFrameform.submit(); 
    self.hiddenFrame.load(function(){ 

     alert(self.hiddenFrame.contents().html()); 
    });      
}); 

这将返回一个空警报。我如何获得#hiddenFrame内容

+0

是你在同一个域下加载的iframe吗?你有没有注意到在JavaScript控制台的任何错误? – fcalderan 2012-01-16 09:14:02

+0

是在同一个域上。控制台返回没有错误 – user1066679 2012-01-16 09:20:11

+0

@FabrizioCalderan请帮助解决此问题...请 – user1066679 2012-01-16 09:27:07

回答

2

你很近。 .contents()方法返回一组DOM元素,所以你需要指定你想返回html的元素。所以,你需要这样一行:

hiddenFrame.contents().find('html').html(); 

我不能完全得到样品流失你上面提供的代码,但我设法得到它具有以下调整工作:

$("#imageFrame").load(function(){ 

    imageFramecontent = $("#imageFrame").contents(); 
    imageFrameupload = imageFramecontent.find(":input[ name = 'fileupload' ]"); 
    imageFrameform = imageFramecontent.find("form#imageForm"); 
    hiddenFrame = imageFramecontent.find("iframe#hiddenFrame"); 

    imageFrameupload.change(function(){ 

     hiddenFrame.load(function(){ 

      alert(hiddenFrame.contents().find('html').html()); 

     }); 
     imageFrameform.submit(); 

    }); 
}); 
+0

您是Genius Goran并感谢您的帮助 您的解决方案已解决了许多问题。谢谢 – user1066679 2012-01-18 06:34:24