在一个项目中,我设法通过phonegap工作来编写文件:
$("#download").live("click", function(){
writeFile();
});
function writeFile(){
$.get("http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34", function(result){
json = result;
removeHTMLTags();
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
});
}
function removeHTMLTags(){
if(json){
var strInputCode = json;
/*
This line is optional, it replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with >
*/
strInputCode = strInputCode.replace(/( )*/g,"");
strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";
});
// strTagStrippedText = '"';
strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
strTagStrippedText = $.trim(strTagStrippedText); // overbodige spaties weghalen
//strTagStrippedText += '"';
}};
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write(strTagStrippedText);
readFile();
// contents of file now 'some sample text'
// writer.truncate(11);
// contents of file now 'some sample'
// writer.seek(4);
// contents of file still 'some sample' but file pointer is after the 'e' in 'some'
//writer.write(" different text");
// contents of file now 'some different text'
}
// Einde FILE WRITER
function fail(error) {
console.log(error.code);
}
在另一个项目中,我做了完全相同的事情,并且它不起作用,我创建了另一个项目,因为我需要重新开始。我是这样做的:
$(".startNew").live("click", function(){
writeFile();
$.mobile.changePage($("#games"));
krijgSpellen();
});
function writeFile(){
navigator.notification.alert("write file");
$.get("http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34", function(result){
json = result;
removeHTMLTags();
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
});
}
function removeHTMLTags(){
if(json){
var strInputCode = json;
/*
This line is optional, it replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with >
*/
strInputCode = strInputCode.replace(/( )*/g,"");
strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";
});
// strTagStrippedText = '"';
strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
strTagStrippedText = $.trim(strTagStrippedText); // overbodige spaties weghalen
//strTagStrippedText += '"';
}};
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write(strTagStrippedText);
readFile();
// contents of file now 'some sample text'
// writer.truncate(11);
// contents of file now 'some sample'
// writer.seek(4);
// contents of file still 'some sample' but file pointer is after the 'e' in 'some'
//writer.write(" different text");
// contents of file now 'some different text'
}
// Einde FILE WRITER
function fail(error) {
console.log(error.code);
}
当我这样做时,它不会写文件,真的很奇怪:
$(".startNew").live("click", function(){
writeFile();
$.mobile.changePage($("#games"));
krijgSpellen();
});
它不会写文件,但是当我这样做时:
$(".startNew").live("click", function(){
writeFile();
});
它确实有效,它会写一个文件
答案 0 :(得分:0)
从您上面的评论中看来,您似乎是动态生成了与.startNew
类的链接,这就是为什么它适用于live
,如果您使用的是 jquery 1.7 + 使用on
或使用delegate
,因为直播是deprecated
$(function(){
$(document).on("click",".startNew", function(){ writeFile(); });
});
或与代表
$(function(){
$(document).delegate(".startNew","click", function(){ writeFile(); });
});