jQuery是一种广泛使用的JavaScript框架,它提供了许多方便的方法来操作HTML和CSS,还可以与服务器进行通信。在此介绍如何使用jQuery来写入txt文件。
$(document).ready(function(){ $("#btn1").click(function(){ var content = "这是写入txt文件的内容"; var filename = "myfile.txt"; download(filename, content); }); }); function download(filename, text) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', filename); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); }
以上是通过jQuery来写入txt文件的代码例子,首先获取到要写入的文件内容和文件名,之后调用download函数并传入参数,该函数会在浏览器中创建一个不可见的链接,并在点击后进行下载操作,最后从DOM中删除该链接元素。
上一篇 jquery 写for
下一篇 html文字环绕图片的代码