ajax读取文本内容(此处的txt文件和html文件处于同级目录)

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
</style>
<title>read file</title>
</head>
<script src="jquery-1.11.2.js"></script>
<script>

            $(function() {
                $("#btn1").click(function(){
                    $.ajax({
                        type: "get",
                        url: "1.txt",
                        dataType: "text",
                        async: true
                    }).done(function (data) {
                        $("#one").html(data.replace(/\r\n/ig, '</br>'));//替换回车行成换行符
                        //console.log(data.replace(/\r\n/ig,'</br>'));
                    });
                });
            });
        </script>
    <body>
        <div id="one" style="width: 300px;background: greenyellow;margin: 10px;;">
        </div>
        <input type="button" id="btn1" value="点击获取"/>
    </body>
</html>

 

posted @ 2017-01-06 11:42  老Fe同学  阅读(8069)  评论(0编辑  收藏  举报