Ajax获取txt固定行

html文件

  <a onclick="ajax();">...</a></li>
  <table class="table tile" id="123">
      <thead>
          <tr>
              <th>Number</th>
              <th>Title</th>
              <th>Url</th>
              <th>Describe</th>
          </tr>
      </thead>
      <tbody>
          <tr>
              <td id="resText_1">Number</td>
              <td id="resText_2">Title</td>
              <td id="resText_3">Url</td>
              <td id="resText_4">Describe</td>
          </tr>
      </tbody>
  </table>

js文件

  function ajax() {
    //声明异步请求对象:
    var xmlHttp = null;
    if (window.ActiveXObject) {
        // IE6, IE5 浏览器
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器
        xmlHttp = new XMLHttpRequest();
    }
    //如果实例化成功,调用open()
    if (xmlHttp != null) {
        xmlHttp.open("get", "links.txt", true); 
        xmlHttp.send();
        xmlHttp.onreadystatechange = doResult; //设置回调函数                 
    }
    function doResult() {
        if (xmlHttp.readyState == 4) { //4表示执行完成
            if (xmlHttp.status == 200) { //200表示执行成功
                //获取txt行数
                var lineArray = xmlHttp.responseText.split('\n');
                for(var i = 0; i<lineArray.length;i++){
                    var line = lineArray[i];
                }
            //分别输出第1,2,3,4行
                $("#resText_1").html(lineArray[0]);
                $("#resText_2").html(lineArray[1]);
                $("#resText_3").html(lineArray[2]);
                $("#resText_4").html(lineArray[3]);
            }
        }else{
            alert("error");
            }
    }
}

txt文件

1
2
3
4

参考文章

https://blog.csdn.net/luyanc/article/details/79470900

posted @ 2020-05-08 11:19  NephKis·浅暮  阅读(121)  评论(0编辑  收藏  举报