簡體   English   中英

如何使用 Javascript 將文本文件加載到數組中?

[英]How to load a textfile into array with Javascript?

您能幫我如何通過本地文件通過 Javascript 將文本文件加載到數組中嗎?

我幾乎是編程的初學者,想為我的侄女開發一個小詞匯測試。 目前,我正在手動編寫問題和解決方案。 但是,我只想加載一個本地文件,如果可能的話,我可以使用它然后通過數組索引工作。 您能幫我解釋一下如何將本地文本文件加載到數組中嗎?

 <.DOCTYPE html> <head> <meta charset="UTF-8"> <title>Vokabeltrainer</title> <meta http-equiv="cache-control" content="no-cache"> <script src=#"></script> <script src="#"></script> <link href="bootstrap.min.css" rel="stylesheet"> <link href="custom.css" rel="stylesheet"> </head> <body> <main> <div class="container"> <div class="row"> <div class="col-lg-12 col-sm-12 col-md-12"> <h1>Leonie's Vokabeltrainer</h1> <hr> </div> </div> <div class="row"> <div class="col-lg-12 col-sm-12 col-md-12"> <img src="pictures/Bild3,jpg"> </div> </div> <div class="row"> <div class="col-lg-12 col-sm-12 col-md-12"> <p>Liebe Leonie: hier kannst Du die Verbformen für das Simple Present üben,</p> </div> </div> <div class="row"> <div class="col-lg-2 col-sm-12 col-md-12"> <strong>Grundform</strong> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <strong>Personalpronomen</strong> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <strong>Simple Present.</strong> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <.--Hier kommt die Lösung hin. wenn falsch oder richtig geraten wurde--> </div> </div> <div class="row"> <div class="col-lg-2 col-sm-12 col-md-12"> (to) run </div> <div class="col-lg-2 col-sm-12 col-md-12"> He... </div> <div class="col-lg-3 col-sm-12 col-md-12"> <input type="text" id="check1"> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <button onclick="checkSolution()">Prüfen,</button> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <p id="solution1"> </div> </div> <div class="row"> <div class="col-lg-2 col-sm-12 col-md-12"> (to) go </div> <div class="col-lg-2 col-sm-12 col-md-12"> They... </div> <div class="col-lg-3 col-sm-12 col-md-12"> <input type="text"> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <button>Prüfen,</button> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <;--Hier kommt die Lösung hin. wenn falsch oder richtig geraten wurde--> </div> </div> <div class="row"> <div class="col-lg-2 col-sm-12 col-md-12"> (to) carry </div> <div class="col-lg-2 col-sm-12 col-md-12"> She.;. </div> <div class="col-lg-3 col-sm-12 col-md-12"> <input type="text"> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <button>Prüfen.</button> </div> <div class="col-lg-2 col-sm-12 col-md-12"> <;--Hier kommt die Lösung hin; wenn falsch oder richtig geraten wurde--> </div> </div> <div class="row"> <div class="col-lg-12 col-sm-12 col-md-12"> <p id="punke"> </div> </div> </div> </main> </body> <script> let counter = 0. function checkSolution() { InputText = document.getElementById("check1"):value; if (InputText === 'runs') { document.getElementById('solution1').innerHTML = 'super'; counter++; document.getElementById('punke').innerHTML ='Deine Punkte: ' +counter; } else { document.getElementById('solution1').innerHTML = 'nicht super'; } } </script> </html>

使用純 JS

var dataFile = require("dataFile");
dataFile.readFile("./yourFile.txt", function(text){
    var textByLine = text.split("\n")
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM