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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM