我将尝试使用jQuery“CSV”插件,文档在此处: http://code.google.com/p/js-tables/wiki/CSV
根据文档所述:
// Convert CSV data into array of arrays
jQuery.csv()("1,2,3\n4,5,6\n7,8,9\n"); // = [ [1,2,3], [4,5,6], [7,8,9] ]
但是当我尝试做类似的事情时,它似乎只把"\n"视为数组元素中间的另一个字符。
我做错了什么吗? 我正在使用jQuery 1.4.2和Firefox 3.5.10进行测试。我也尝试过使用jQuery 1.3,结果相同。
如果这是插件的问题,那么有人可以建议另一个用于读取CSV的插件吗? 我的最终目标是将CSV从字符串转换为HTML表格;我能找到的唯一一个专门做这个的插件需要CSV来自文件,这对我的任务不是理想的选择。
这是一个我准备的最小测试页面,它说明它没有将行分隔成子数组:
<html>
<head>
<title>CSVtest</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.csv.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var lines = $.csv()("a,b\nc,d");
alert(lines[0][1]); // Displays: b
// c instead of the expected b
});
</script>
</head>
<body>
</body>
</html>
"
和'
之间没有任何区别。 - Andy E