Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将元素转换为可从localStorage存储和还原的字符串_Javascript_Jquery_Html_Dom_Local Storage - Fatal编程技术网

Javascript 如何将元素转换为可从localStorage存储和还原的字符串

Javascript 如何将元素转换为可从localStorage存储和还原的字符串,javascript,jquery,html,dom,local-storage,Javascript,Jquery,Html,Dom,Local Storage,我的计划是,一旦我的元素被在线应用程序读取一次,就将它们存储在localStorage中——我想如果我能让它正常工作,它将大大加快我的应用程序的速度。因此,我提出了以下内容,但内容似乎要么被存储,要么返回为空。现在我认为一个元素基本上是一个字符串,所以我不需要对它做任何事情来将它存储在localStorage中,但显然不是。有人知道是否有可能实现我想要的吗 如果您想让它转一转,那么您需要一个包含html的小文件来传递给它。内容并不重要。第一次运行时,如果内容的计算结果为false。下一次为tru

我的计划是,一旦我的元素被在线应用程序读取一次,就将它们存储在localStorage中——我想如果我能让它正常工作,它将大大加快我的应用程序的速度。因此,我提出了以下内容,但内容似乎要么被存储,要么返回为空。现在我认为一个元素基本上是一个字符串,所以我不需要对它做任何事情来将它存储在localStorage中,但显然不是。有人知道是否有可能实现我想要的吗

如果您想让它转一转,那么您需要一个包含html的小文件来传递给它。内容并不重要。第一次运行时,如果内容的计算结果为false。下一次为true,我添加了.clear以使其成为替换。第三次showSource函数将填充id=localor元素,该元素需要与id=content元素或您选择填充的任何其他元素分开,并在from文件之后显示null

function showSource(source) {   //Function and related element only here for testing
    $("#localor").html("<p>"+source+"</p>");
}

//local is the name of the localStorage element
//id is the id of the element in the main form
//source is where the content for the element originates.
    function loadElement(local, id, source) {   

//This is the generic script for attempts to load an element.

        content = localStorage.getItem(local); //Try to load from local storage
    if (content) {//If found
        $(id).html(content);
        showSource("from Local " + content);    //Added only for testing
        localStorage.clear();   //Added only for testing
        //load into the #content element
    } else {//Not found
        $(id).load(source);
        showSource("from File " + content); //Added only for testing
        //so load it from the server..
        localStorage.setItem(local,$(id).html());
        //then save it to the local storage, so we don't have to do this again any time soon.
    }
}


$(document).ready(function() {
    loadElement("login.1001", "#content", "login.html");
});
请帮忙


谢谢。

您所做的工作不起作用的原因是您正在加载项目之前保存它。load是异步工作的,就像所有合理的ajax调用一样。该过程在调用load时开始,但稍后完成。然后,您的代码继续并获取它认为是内容的内容并保存它,但内容还没有出现

改用回调函数:

$(id).load(source, function() {
    showSource("from File " + content); //Added only for testing
    //so load it from the server..
    localStorage.setItem(local,$(id).html());
    //then save it to the local storage, so we don't have to do this again any time soon.
});

但是……你不是在重新设计浏览器缓存吗?

我相信你是在重新设计浏览器缓存……这是许多情况之一,在这些情况下,使用调试器遍历代码、检查变量以及执行这些操作,都会告诉你答案,或者至少让你走上通往它的道路。您的浏览器内置了调试器。那是你的朋友-萤火虫是用来Firefox@pheromix:是的,尽管现在连Firefox都有一个内置的。这可能是一个很好的建议。我还没有弄清楚如何调试这些语言中的任何一种。下次遇到严重问题时我会调查的。谢谢。据我所知,浏览器缓存不是很持久,但我想你是对的。谢谢你的帮助。我仍然习惯于什么是同步运行的,什么是异步运行的。缓存的循环速度确实相当快。浏览器的默认缓存大小非常小,但我仍然会确保缓存友好,而不是将页面存储在本地存储中。不仅是因为如果每个人都开始这样做,本地存储也会开始出现与缓存相同的问题。这肯定是一个风险。不幸的是,即使删除了“content”一词,如果我按照建议在回调中对其进行编码,脚本也不会运行。我很困惑,这是我早期经常出现的状态。我尝试将参数添加到回调函数中,但没有效果。我缺少一个括号和分号。现在可以了。谢谢。这将显著提高我的应用程序的性能: