使用JavaScript保存文本文件到本地的两种方法
chenqiangdage 人气:0一段使用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 rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css" rel="external nofollow" > <script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script> <script type='text/javascript' src='//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 name="description" content="A simple HTML5 Template"> <meta name="author" content="dron"> <meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <title>保存文本文件到本地</title> <link href="http://ucren.com/static/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <script src="http://ucren.com/static/jquery/2.2.3/jquery.min.js"></script> <script src="http://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 class="ucren-toolbar-placeholder" style="height:3000px;"></div><scri' +'pt src="//ucren.com/ucren-toolbar/scripts/main.js"><\/script>')}(this); siteToolbar(); </script> <div class="container-fluid"> <div class="row"> <div class="col-md-3"> <div class="form-group"> <label>文件内容</label> <textarea class="form-control" rows="4" id="content">测试文本,你可以随便修改,完事后点保存。</textarea> </div> <div class="form-group"> <label>文件名</label> <input type="text" class="form-control" value="test.txt" id="file-name"> </div> <a id="save-btn" class="btn btn-default" 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>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
加载全部内容