假设你有一个url (https://www.google.fr/)
您不希望在执行该页面的JavaScript后还拥有该页面的HTML代码。
想象一下javascript执行之前的基本HTML是这样的:
<html>
<head>
<title>Super test</title>
</head>
<body>
<script>
var i = document.createElement("div");
i.className = "test";
document.bodu.appendChild(i);
</script>
</body>
</html>
我需要的是一个代码(一种方式)来得到这个结果:
<html>
<head>
<title>Super test</title>
</head>
<body>
<div class="test"></div>
<script>
var i = document.createElement("div");
i.className = "test";
document.body.appendChild(i);
</script>
</body>
</html>
我试过这个选项:
- stackoverflow(但没有成功使用)
- PHP PhantomJS(但当我试图使用它,它给我javascript执行前的代码)。
您需要等待DOM准备好。https://api.jquery.com/ready/
用body代替bodu:)
如果您希望在javascript执行后查看文档的html,请使用document.documentElement.innerHTML .
试试这个,我在你的原始页面中添加了setTimeout,它会打开一个警告框,并显示修改后的带有"test"div的页面。
<html>
<head>
<title>Super test</title>
</head>
<body>
<script>
var i = document.createElement("div");
i.className = "test";
document.body.appendChild(i);
</script>
<script>
setTimeout(function() {
alert(document.documentElement.innerHTML);
},2000);
</script
</body>
</html>