如何将数据从文本文件加载到javascript数组? [英] how to load data from text file to javascript array?
问题描述
我有一个名为 items = [apple,mango,cherry]的数组;
<我想知道如何从文本文件加载数组数据而不是声明它?文本文件数据存储如apple,mango,cherry,...
此外,如何在此文本文件的末尾添加一个项目,例如在cherry之后添加orange?
项= [ 苹果, 芒果, 樱桃];
if(items.indexOf(myVariable2)== -1){
//找不到,所以输出
t ++;
document.myform3.outputtext3.value + = myVariable2 +\ n;
}
抱歉,但我不相信这很简单。出于安全原因,浏览器限制对本地驱动器(以及服务器驱动器)的访问。
但是使用jQuery访问文本文件的一种方法是
jQuery.get('http://localhost/foo.txt',function(data){
var myvar = data;
});
i have an array called items=["apple","mango","cherry"];
i wonder how i can load the array data from text file instead of declaring it?the text file data is stored like this "apple","mango","cherry",...
furthermore, how to add to the end of this this text file an item for example add "orange" after "cherry"?
items=["apple","mango","cherry"];
if (items.indexOf(myVariable2) == -1) {
// not found, so output it
t++;
document.myform3.outputtext3.value +=myVariable2+"\n";
}
Sorry, but I don't believe it's quite that simple. Browsers restrict access to local drives (and to server drives) for security reasons.
But one way to access the text file using jQuery would be
jQuery.get('http://localhost/foo.txt', function(data) {
var myvar = data;
});
这篇关于如何将数据从文本文件加载到javascript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!