从数据文件导入数组
Import from data file into array
下面的代码显示应该导入值从temps.txt文件,但从times数组看起来是空的,怎么了?
我只得到"楠"作为输出。
temps.txt文件如下所示:
21.5
22.3
... etc
这是我的源代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>load demo</title>
<style>
body {
font-size: 16px;
font-family: Arial;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<script>
var times = [];
$.get('temps.txt', function(data) {
times = data.split("'n");
});
for (var i in times)
{
document.write(times[i] + "<BR />");
}
</script>
</html>
类似这样的东西:
1) 将循环移到$.get
回调中。
2) 使用数组建立字符串
3) 使用join
将其附加到body元素。
$.get('temps.txt', function(data) {
times = data.split("'n");
var html = [];
for (var i in times) {
html.push(times[i] + '<br/>');
}
$('body').append(html.join(''));
});
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- knockoutjs可观察数组
- 通过内部数组将JSON数据导入D3.js+Google Map
- 如何在Javascript中从Json数组导入值
- 如何将数据从一个表导入另一个表(使用数组)
- 导入 JSON 以用作 GoogleMaps Marker 的数组
- 将 Excel 文件导入和导出到 JavaScript 数组
- 从数据文件导入数组
- 使用 javascript 将数据从文本文件导入数组
- 如何在javascript中将数组导入到函数中
- 使用ajax导入数组
- 如何收集表单属性名称和“;value"使用javascript从表单的特定部分导入数组
- 将AJAX数据导入数组.[JavaScript]
- JS在文件之间导入数组
- 导入php数组到javascript数据集