[英]reading a txt file line by line using JavaScript
我可以使用以下命令打印整個文件內容:
write(document.getElementById('iframe').contentDocument.body.firstChild.innerHTML);
但是,如何逐行檢索和打印文件內容?
您可以使用split函數和定界符“ \\ r \\ n”或“ \\ n”提取加載的內容中的所有單獨的行,然后對它們執行任何操作:
var text = document.getElementById('iframe').contentDocument.body.firstChild.innerHTML;
var lines = text.split("\n"); // this will extract all the lines
for (i=0; i<lines.length; i++) { // this will move over all the lines
var current_line = lines[i]; // this contains every line at its turn
// do here what ever you want to do with current_line
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.