javascript 从 .txt 文件加载数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9127447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:41:02  来源:igfitidea点击:

Loading an array from .txt file

javascriptjquery

提问by David Debnar

I need to load an array from a .txt file. It has few strings separated by commas (,).

我需要从 .txt 文件加载一个数组。它有几个由逗号 ( ,)分隔的字符串。

So far I have found this.

到目前为止,我已经找到了这个

That guy splits it with .split function but I have a problem. He uses an input to upload the file, however, I need to link the file to the HTML and than split it into an array and use it.

那家伙用 .split 函数将其拆分,但我遇到了问题。他使用输入上传文件,但是,我需要将文件链接到 HTML,然后将其拆分为数组并使用它。

回答by Alexander St?ver

Just make sure the text file is accessible through an HTTP request on the same domain and you can read it fine. Here's an example where the text file is located in the webroot.

只需确保文本文件可通过同一域上的 HTTP 请求访问,您就可以很好地阅读它。这是文本文件位于 webroot 中的示例。

    $(function(){
        $.get('/whatever.txt', function(data){
            var array = data.split(',');
            console.log(array);
        });
    });

回答by S?ren Titze

You can read html files locally since html5. Check the html5 API and some examples here. Then use Alexanders answer to parse it.

从 html5 开始,您可以在本地读取 html 文件。在此处查看 html5 API 和一些示例。然后使用亚历山大的回答来解析它。

EDIT:hereand hereare some good and short examples on how to use it.

编辑:这里这里有一些关于如何使用它的简短示例。