假設我有iframe中包含sample.html如何從iframe獲取所有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>
我想獲得的所有內容包括<!DOCTYPE html> ...</html>
。我試圖尋找角落找尋網絡,但我發現只有使用
var cont = document.getElementById('myframe').contentWindow.document;
console.log(cont.body);
我不想使用AJAX來獲取內容文件得到的主體內容。 如何從iframe
獲取全部內容包括html標記?
'的console.log(新的XMLSerializer() .serializeToString(cont));'解決我的問題。謝謝。 –