首页 > 解决方案 > 如何下载当前网页

问题描述

我想添加一个按钮来下载我的网页的 html 源代码,我能够做到,但问题是当我添加 localhost url 时它可以工作,但是当我添加 live url 时它打开的是 url 而不是下载它。到目前为止我试过这个

 <a download  href="http://localhost/test-new/">Download Source Code</a>

我也试过这个

<button onclick="clicker()">Click me</button>



<script type="text/javascript">

function clicker() {
    var anchorTag = document.createElement('a');
    anchorTag.href = "http://localhost/test-new/";
    anchorTag.download = "download";
    anchorTag.click();


    var element = document.getElementById('divContainer');
    element.appendChild(anchorTag);
}

</script>

这两者都在本地主机中工作,但不适用于实时网址。请帮我

标签: javascripthtml

解决方案


您可以使用

document.querySelectorAll('*')

获取网页的源代码

console.log(document.querySelectorAll('*'))


推荐阅读