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

JavaScript021

怎么用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>

<!DOCTYPE html>

<html>

  <head>

    <title>文件示例</title>

    <meta name="name" content="content" charset="utf-8">

  </head>

  <body>

      <input type="file" id="file" />

      <input type="button" onclick="readText()" value="File Button">

      <div id="tt">

  

      </div>

  </body>

</html>

<script charset="utf-8">

window.onload=function () {

  if(typeof(FileReader)=="undefined")

  {

    alert("你的浏览器不支持文件读取")

    document.write("")

  }else

  {

    alert("你的浏览器支持文件读取")

  }

}

  function readText() {

      var file=document.getElementById("file").files[0]

      var reader=new FileReader()

      reader.readAsText(file)

      reader.onload=function(data)

      {

        var tt=document.getElementById("tt")

        tt.innerHTML=this.result

      }

    }

  

</script>

第一个是用JS代码写的,网上应该有下载的。你可以去找找。

第二个是用表单做的,一个文本框,一个密码框,一个下拉菜单,一个按钮,把他们放在一个大的表单里面,然后在css样式里面定义表单的样式,把边框改成相应的颜色即可。

第三个是排版布局吗???感觉好像是用的div,然后里面加的table/???