之前同事讓我?guī)兔匆幌?log日志文件,功能是上傳日志文件到頁(yè)面上,再使用任一工具把日志文件顯示到頁(yè)面上。下面的代碼只是簡(jiǎn)單的一個(gè)功能demo,顯示的是自己的一個(gè)Git日志,功能完全滿足了我自己的需要。文章最后面是整個(gè)代碼,復(fù)制代碼后直接保存為.html文件就可以了

源代碼:
<!DOCTYPE html><html lang="en">
<head> <meta charset="UTF-8">
????<meta name="viewport" content="width=device-width, initial-scale=1.0">
????<meta http-equiv="X-UA-Compatible" content="ie=edge">
????<title>Document</title>
????<style> #text{ width: 1000px; height: 800px; } </style>
</head>
<body>
????<input type="file" id="file-input" />
????<div id="file-content"></div>
????<textarea id='text'> </textarea>
?</body>
<script>
????????function readSingleFile(e) {
//獲取選擇的文件對(duì)象
var file = e.target.files[0]; if (!file) { return; }
// 創(chuàng)建FileReader對(duì)象
var reader = new FileReader();
// load 回調(diào)
reader.onload = function(e) { var contents = e.target.result;
// alert(contents)
// 內(nèi)容處理
document.getElementById("text").value = contents; };
// 讀取文件
reader.readAsText(file,"utf-8"); }
// 添加監(jiān)聽(tīng)事件
var fileInput = document.getElementById("file-input"); fileInput.addEventListener("change", readSingleFile, false);
</script></html>