js获取代码片下载并生成HTML
程序员文章站
2022-06-19 16:13:13
好好学习 ,天天向上。Are you ready?例子:js获取代码片下载并生成HTML js获取代码片下载并生成HTML
好好学习 ,天天向上。Are you ready?
例子:js获取代码片下载并生成HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js获取代码片下载并生成HTML</title>
</head>
<body>
<button id="btn">下载HTML</button>
</body>
<script>
let text = '<p>下载HTML成功</p>'
let btn = document.getElementById('btn')
btn.addEventListener('click', handleClick);
function handleClick() {
funDownload(text, 'download.html')
window.open('download.html')
}
function funDownload(content, filename) {
var eleLink = document.createElement("a");
eleLink.download = filename;
eleLink.style.display = "none";
var blob = new Blob([content]);
eleLink.href = URL.createObjectURL(blob);
document.body.appendChild(eleLink);
eleLink.click();
document.body.removeChild(eleLink);
}
</script>
</html>
本文地址:https://blog.csdn.net/weixin_43654258/article/details/111028802