我是html javascript初学者,我只需要在一个位置读取文本文件的内容,并将该文件中的每一列存储在html中的数组(有3列)中,并使用javascript来执行此操作。< / p>
我遇到了2个错误http://prntscr.com/6ilvhx:
(1)ReferenceError:未定义require
(2)ReferenceError:未定义readfile
我的代码是:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script>
console.log("just a check1");
var fs = require('fs');
console.log("just a check2");
function readLines(input, done) {
var arr = [];
var remaining = '';
input.on('data', function (data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function () {
if (remaining.length > 0) {
func(remaining);
done(arr);
}
});
function func(data) {
arr.push(data.split(/\s+/g));
}
}
var input = fs.createReadStream('Content\TextFile\distinctGimeiNo.txt');
readLines(input, done);
function done(arr) {
var obj = {};
var key1 = arr[0][0];
var key2 = arr[0][1];
var key3 = arr[0][3];
obj[key1] = [];
obj[key2] = [];
obj[key3] = [];
arr.shift();
arr.forEach(function (item) {
obj[key1].push(item[0]);
obj[key2].push(item[1]);
obj[key3].push(item[2]);
});
console.log('X:', obj['X']);
console.log('Y:', obj['Y'])
console.log('Z:', obj['Z'])
}
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
如何使它工作?我在调试http://prntscr.com/6ilvhx
时发现了这一点更新代码:(添加库后)
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src=http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js>
console.log("just a check1");
var fs = require('fs');
console.log("just a check2");
function readlines(input, done) {
var arr = [];
var remaining = '';
input.on('data', function (data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index >
-1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function () {
if (remaining.length > 0) {
func(remaining);
done(arr);
}
});
function func(data) {
arr.push(data.split(/\s+/g));
}
}
var input = fs.createReadStream('Content\TextFile\distinctGimeiNo.txt');
readLines(input, done);
function done(arr) {
var obj = {};
var key1 = arr[0][0];
var key2 = arr[0][1];
var key3 = arr[0][2];
obj[key1] = [];
obj[key2] = [];
obj[key3] = [];
arr.shift();
arr.forEach(function (item) {
obj[key1].push(item[0]);
obj[key2].push(item[1]);
obj[key3].push(item[2]);
});
console.log('X:', obj['X']);
console.log('Y:', obj['Y'])
console.log('Z:', obj['Z'])
}
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
答案 0 :(得分:2)
无法在浏览器中使用node.js脚本,请以节点启动..:)
您可以使用jQuery在客户端执行此操作:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script>
console.log("just a check1");
$.ajax({
type:'GET',
url:'./hello.txt',
dataType: 'text',
}).success(function (data){
s = data.substr(32);
var s2 = s.replace(/\r\n/g," ");
var array = s2.split(' ');
var col1 =[];
var col2 =[];
var col3 =[];
var j=0;
for(var i = 0; i <= array.length-3; i=i+3){
col1[j]=array[i];
col2[j]=array[i+1];
col3[j]=array[i+2];
j++;
};
console.log(col1,col2, col3);
})
</script>
</head>
<body>
<h1>//hello world !</h1>
</body>
</html>
$ .ajax允许您使用指定网址获取数据。 通常,这就是你想要的那样