使用JavaScript保存文本文件到本地的两种方法

一段使用javascript保存文件的代码。这里方法可以保存指定id元素下的所有html内容;不过这个方法只支持IE浏览器。

function createHtml() { try { save_record("index1", $("#yhtcprediv").html()); } catch (e) { alert(e); } } function save_record(filename, content) { //打开新窗口保存 var winRecord = window.open('about:blank', '_blank', 'top=500'); winRecord.document.open("text/html", "utf-8"); winRecord.document.write(" <div class=\"introBox section package boxBg02\" id=\"yhtcprediv\">" + content + "</div>"); winRecord.document.execCommand("SaveAs", true, filename + ".html"); winRecord.close(); }

简单好用的JS保存文本文件到本地

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link href="https://g.alicdn.com/msui/sm/0.6.2/css/sm.min.css" > <script type='text/javascript' src='https://g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script> <script type='text/javascript' src='https://g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script> </head> <body> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="A simple HTML5 Template"> <meta content="dron"> <meta content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0, maximum-scale=1.0"> <meta content="yes"> <meta content="black"> <title>保存文本文件到本地</title> <link href="https://ucren.com/static/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ucren.com/static/jquery/2.2.3/jquery.min.js"></script> <script src="https://ucren.com/static/bootstrap/3.3.6/js/bootstrap.min.js"></script> <style> .container-fluid{ margin-top: 20px; } </style> </head> <body> <script> void function(a){a.siteToolbar=function b(a){b.setting=a||{}},document.write ('<div></div><scri' +'pt src="https://ucren.com/ucren-toolbar/scripts/main.js"><\/script>')}(this); siteToolbar(); </script> <div> <div> <div> <div> <label>文件内容</label> <textarea rows="4">测试文本,你可以随便修改,完事后点保存。</textarea> </div> <div> <label>文件名</label> <input type="text" value="test.txt"> </div> <a download="test.txt" role="button">保存</a> </div> </div> </div> <script> void function(){ var content, fileName, saveBtn; content = document.querySelector( '#content' ); fileName = document.querySelector( '#file-name' ); saveBtn = document.querySelector( '#save-btn' ); content.addEventListener( 'change', function self(){ saveBtn.setAttribute( 'href', 'data:text/paint; utf-8,' + content.value ); return self; }() ); fileName.addEventListener( 'change', function self(){ saveBtn.setAttribute( 'download', fileName.value ); return self; }() ); }(); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-4190821-1', 'ucren.com'); ga('send', 'pageview'); </script> </body> </html> </body> </html>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1cca0fe245dd2c07ce6663eb7a8d8850.html