[英]Put text from a .txt file into <p> tags using JavaScript
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
. 我有一个正在使用
PhoneGap
, HTML
和JavaScript
开发的应用程序,到目前为止,我有该应用程序下载文件并将其读取到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. 我现在想将其读取到指定的
<p>
标记中,但是当我尝试一种从LocalFileSystem
读取文件时知道可以工作的技术时,它将无法工作。 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);
}
The function readDownloadFile()
is supposed to read the text in the .txt file readme.txt
into the <p>
tags with the id mytext
. 功能
readDownloadFile()
应该读取.txt文件文本readme.txt
到<p>
与所述ID标签mytext
。 However, when I try and run it nothing happens. 但是,当我尝试运行它时,没有任何反应。 Any ideas on what I'm doing wrong?
关于我在做什么错的任何想法吗?
I would recommend you, to use a simple ajax call to 我建议您使用简单的Ajax调用来
http://dl.dropbox.com/u/97184921/readme.txt http://dl.dropbox.com/u/97184921/readme.txt
and pass it into your p tag. 并将其传递给您的p标签。
like 喜欢
$('#mytext').load('http://dl.dropbox.com/u/97184921/readme.txt', function(){
alert("i am done")
});
check http://api.jquery.com/load/ 检查http://api.jquery.com/load/
This needs to be invoked by a click event or something. 这需要通过单击事件或其他事件来调用。 (In your example it's not invoked)
(在您的示例中未调用)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.