我有一个txt文件,约有754线。每一行都有一个模式是这样的:
I have a .txt file, with around 754 lines. Each line has a pattern like this:
第1部分文字#第二部分的文字#3部分文字或数字
1st part text#2nd part text#3rd part text or number
包括hashtag是分隔符!
The hashtag is the delimiter!
我想要做的是得到()或read()将这个文本文件,并取回一个随机行从它,所有的模式。 但它必须是一个随机线。 因此,我可以打印或追加HTML到一个div。它应该是这个样子:
What I'm trying to do is to get() or read() this text file, and retrieve a random line from it, with all the pattern. But it has to be a random line. So I can print it or append html to a div. It should look something like this:
第一部分从行 第二部分从同一个行 第三部分从同一个行它有这种模式,因为也许我会需要检索的模式只是其中的一部分。
It has this pattern, because maybe I'll have to retrieve only one part of the pattern.
我不知道,如果一个文本文件是正确类型的文件拉从数据....但其仅80KB。 有没有办法做到这一点?可能有人给我个忙吗?我其实是一个设计师,这件事是推动我疯了。
I don't know if a text file is the right type of file to pull data from.... But its only 80kb. There is any way to do this? Could someone give me a hand? I'm actually a designer, this thing is driving me crazy.
我该怎么...试图指望有多少行的TXT都有,这样我就可以得到一个随机行。它不工作。是否文本文件中必须有\ñ在每一行的结束?
What I got... Trying to count how many lines the txt have, so I can get a random line. It doesn't work. Does the text file must have \n on the end of each line?
$.get('txt/messages.txt', function(txt) {
var lines = txt.responseText.split("\n");
for (var i = 0, len = lines.length; i < len; i++) {
save(lines[i]);
}
});
我使用jQuery 1.11.1和jQuery移动1.4.4
I'm using jQuery 1.11.1, and jQuery mobile 1.4.4
这是例子如何拉出一个随机行从TXT响应:
This is example of how to pull out a random line from the txt response:
$.get('txt/messages.txt', function(txt) {
var lines = txt.responseText.split("\n");
var randLineNum = Math.floor(Math.random() * lines.length);
save(lines[randLineNum]); // random line from the text file
});
在那里,您可以根据分隔符拆分线(#
):
lines[randLineNum].split("#");
下面是一个的jsfiddle 的例子。希望这有助于!
Here's a jsfiddleexample. Hope this helps!