2011-06-25 54 views
1

main.html中有这样的代码:获取帧内容与jQuery

<iframe id="myframe" src="myframe.html"></iframe> 

,我这触发代码中main.html中:

alert($('#myframe').contents().find('#mypage').contents().find('html').html()); 

myframe.html有这样的代码:

<frameset> 
<frame id="mypage" src="mypage.html"> 
</frameset> 

的mypage.html具有所有这些代码:

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
hello world! 
</html> 

我想从main.html中页面内获得的mypage.html的所有HTML代码,但我失败了。我做这件事的方式有什么问题?

+0

请考虑您的提取确切的问题,并要求它。 “不能使它工作”不是一个问题。 – ayckoster

回答

0

从技术上讲,我不认为你可以这样做的框架,因为这将是一个安全问题。

如果你确实需要加载ajax,你可以加载内部框架的源代码。但它肯定不会生活

0

首先你需要在main.html中包含jquery库。 其次, 铬返回一个错误:

Unsafe JavaScript attempt to access frame with URL file:///../myframe.html from frame with URL file:///../main.html. Domains, protocols and ports must match.

所以这是一个安全问题

+0

当两个文件位于同一个域中时,这确实起作用。 – Jan

0

我面临同样的问题。

我能解决它。

在我的例子:

myframe = ReportFrameReportViewerControl 
mypage = report 

这是代码来获取HTML(注意,所有网站必须在同一个域):

function bla() 
{ 
    var str = 
    $(
      $(
       $("#ReportFrameReportViewerControl")[0].contentWindow.document 
      ) 
      .find("#report")[0].contentWindow.document 
     ).find("html").html() 
     ; 

     alert(str); 
} 

你可能需要将其放入到一个按钮,你可以按下一次页面加载:

<input type="button" value="test" onclick="bla();" /> 

此外,如果你想要自动获取html在页面加载完成后,您需要注册myframe & mypage的onload函数。

像这样(更换BLA setTableSize):

$(document).ready(function() 
{ 
    $('#ReportFrameReportViewerControl').load(function() 
    { 
     //setNewHeight(); 
     //alert("Loading"); 

     setTableSize(); 

     $(
      $("#ReportFrameReportViewerControl")[0].contentWindow.document 
     ) 
     .find("#report").load(function() 
     { 
      //alert("load report"); 
      setTableSize(); 
     } 
     ); // End load #report 


    }); // End Function load #ReportFrameReportViewerControl 


}); // End Function document.ready