jQuery获取整个iframe源代码,包括外部注释

时间:2016-01-27 06:11:37

标签: javascript jquery html iframe

问题How do I get the entire page's HTML with jQuery?可能看起来很相似,这有助于从<html><!DOCTYPE>获取数据。但此外,我还要求获取在<html>标记之前保留的任何注释。

我使用jQuery在srcdoc属性的帮助下显示页面。我目前获取数据的代码是

$("#myiframe").contents().find('html')[0].outerHTML;

以及this answer中的摘要以获取<!DOCTYPE html>

示例用例:

<!-- 
  This comment will be used for further processing
--> 
<!DOCTYPE html>
<html lang='en'>
<head>
...
</head>
<body>
...
</body>
</html>
<!-- 
  This comment will be used for further processing
-->

目前的输出仅从<!DOCTYPE html></html>。预计会在外面发表评论。

1 个答案:

答案 0 :(得分:0)

查看jQuery的$.get函数。这基本上会返回您使用普通GET请求检索的所有内容。 https://api.jquery.com/jquery.get/

试试这个:

$('iframe#myiframe').load(function() {
  getFrameContent(this.contentWindow.location);
});

function getFrameContent(windowURL) {
   $.get( windowURL, function( data ) {
     //where 'data' is the page contents
     $( "#whateverElementYouWant" ).html( data );
  });
}
相关问题