将.txt文件中的文本放入< p>标签使用JavaScript [英] Put text from a .txt file into <p> tags using JavaScript
问题描述
我有一个使用 PhoneGap
, HTML
和 / code>,到目前为止,我有应用程序下载一个文件,并阅读它到
console.log
。我现在想要读取它到指定的< p>
标签,但当我尝试一个技术,我知道工作,当从 LocalFileSystem
它不工作。这里是代码;
I have an app I'm developing using PhoneGap
, HTML
and JavaScript
and so far I have the app downloading a file and reading it to the console.log
. I now want to read it into specified <p>
tags but when I try a technique I know to work when reading a file from the LocalFileSystem
it doesn't work. Here's the code;
function downloadfile() {
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://dl.dropbox.com/u/97184921/readme.txt");
fileTransfer.download(
uri,
'/Users/administrator/Library/Application Support/iPhone Simulator/6.1/Applications/AF96D141-0CE5-4D60-9FA8-8A8F9A999C81/Documents/readme.txt',
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}
);
}
function readDownloadedFile() {
myFileSystem.root.getFile("readme.txt", {create: false, exclusive: false}, gotDownloadFileEntry, fail);
var myDownload = document.getElementById("mytext");
myDownload.innerText = text;
}
function gotDownloadFileEntry(fileEntry) {
console.log(fileEntry);
fileEntry.file(gotFile, fail);
}
函数 readDownloadFile()
应将.txt文件中的文本 readme.txt
读入< p>
id mytext
。但是,当我尝试运行它没有什么发生。关于我做错了什么想法?
The function readDownloadFile()
is supposed to read the text in the .txt file readme.txt
into the <p>
tags with the id mytext
. However, when I try and run it nothing happens. Any ideas on what I'm doing wrong?
推荐答案
我会建议你,使用一个简单的ajax调用
I would recommend you, to use a simple ajax call to
http://dl.dropbox.com /u/97184921/readme.txt
并将其传送到您的p标签。
and pass it into your p tag.
像
$('#mytext').load('http://dl.dropbox.com/u/97184921/readme.txt', function(){
alert("i am done")
});
检查 http://api.jquery.com/load/
这需要由点击事件或其他事件调用。 (在您的示例中,它不会被调用)
This needs to be invoked by a click event or something. (In your example it's not invoked)
这篇关于将.txt文件中的文本放入< p>标签使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!