12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style>
- body{
- background: #00FFFF;
- }
- </style>
- </head>
- <body>
- <!--下载按钮-->
- <button onclick="downloadDivContent('#test123')">下载</button>
- <div id="test123">
- <h1>测试</h1>
- <p>测试内容</p>
- </div>
- <script>
- function downloadDivContent(selector) {
- // 获取目标div的内容
- var targetDiv = document.querySelector(selector).innerHTML;
- // 创建Blob对象
- var blob = new Blob([targetDiv], { type: 'text/html' });
- // 创建下载链接
- var downloadLink = document.createElement('a');
- downloadLink.href = URL.createObjectURL(blob);
- // 设置下载链接的属性
- downloadLink.download = 'div_content.html'; // 设置文件名
- downloadLink.style.display = 'none'; // 隐藏下载链接
- // 将下载链接添加到页面中
- document.body.appendChild(downloadLink);
- // 模拟用户点击下载链接
- downloadLink.click();
- // 清理下载链接
- document.body.removeChild(downloadLink);
- }
- </script>
- </body>
- </html>
|