function download(text, title) {
let a = document.createElement('a')
a.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)
a.download = title
a.click()
}
title
不会像a标签下载图片视频一般存在跨域问题,但 title
需要自行添加文件后缀 .txt
。
此外,如果需要在内容中输入回车换行:
windows下的文本文件换行符: \r\n
,
linux/unix下的文本文件换行符: \r
,
Mac下的文本文件换行符: \n
。