假设我有 iframe 包含sample.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="import" href="sample.html">
</head>
<body>
<p>top</p>
<iframe src="sample.html" frameborder="0" id="myframe"></iframe>
<p>bottom</p>
<textarea cols="30" rows="10"></textarea>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想获得所有内容,包括<!DOCTYPE html> ...</html>
. 我尝试在网络上搜索,但我发现只能使用从正文获取内容
var cont = document.getElementById('myframe').contentWindow.document;
console.log(cont.body);
Run Code Online (Sandbox Code Playgroud)
我不想使用 AJAX 来获取内容文件。如何从iframe
包含html标签获取所有内容?
尝试访问cont.documentElement.innerHTML
以获取标签的内容<html>
。
如果您还想要<html>
标签,请使用cont.documentElement.outerHTML
。
要获取文档类型信息,请尝试使用cont.doctype
.
var cont = document.getElementById('myframe').contentWindow.document;
console.log(cont.documentElement.outerHTML, cont.doctype);
Run Code Online (Sandbox Code Playgroud)
XMLSerializer
如果浏览器支持,您也可以使用:
console.log(new XMLSerializer().serializeToString(cont));
归档时间: |
|
查看次数: |
3999 次 |
最近记录: |