怎么用JS读写本地文本文件

怎么用JS读写本地文本文件,第1张

采用jquery的ajax方式进行文件信息读取

代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<html>

<head>

<script type="text/javascript" src="/jquery/jquery.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$("#b01").click(function(){

htmlobj=$.ajax({url:"/jquery/test1.txt",async:false})

$("#myDiv").html(htmlobj.responseText)

})

})

</script>

</head>

<body>

<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>

<button id="b01" type="button">改变内容</button>

</body>

</html>

在浏览器中做不到,使用javascript服务器端运行环境可以做到,比如可以使用node.js来做

如:

fs.writeFile('xxx.txt', 'Hello Node', function (err) {

if (err) throw err

console.log('It\'s saved!')

})

fs.readFile('xxx.txt', function (err, data) {

if (err) throw err

console.log(data)

})


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11747627.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存