代码如下:
<html>
<body>
<textarea id="content">Type code here</textarea>
<p><a id="save-btn" href="" download="noname.txt">Save</a></p>
<script>
var content, btn;
content = document.querySelector( "#content" );
btn = document.querySelector( "#save-btn" );
content.addEventListener( "change", function(){
var f = function(){
btn.setAttribute( "href", "data:text/paint; utf-8," + content.value );
}
return f(), f;
}() );
</script>
</body>
</html>
打开这个html,显示如下。随便在textarea里维护一些内容:
点击save,会自动将其内容作为text文本下载:
文本文件打开如下: