在一个项目中,我可以写一个文件,但在另一个项目我可以't、 真的很奇怪

In one project I can write a file but in the other I can't, really strange

本文关键字:我可以 项目 一个 真的 文件 另一个      更新时间:2023-09-26

在一个项目中,我设法用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(/(&nbsp;)*/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(/(&nbsp;)*/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();      
                              });

它确实起作用,并写入一个文件

从您上面的评论来看,您似乎正在动态生成与类.startNew的链接,这就是为什么它与live一起工作,如果您使用的是jquery 1.7+使用on,或者使用delegate因为live被弃用

$(function(){
 $(document).on("click",".startNew", function(){ writeFile(); });
});

或代表

$(function(){
     $(document).delegate(".startNew","click", function(){ writeFile(); });
    });