javascript JQuery 设置本地存储变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17758135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery setting a local storage variable
提问by Marshall Tigerus
I'm having some trouble getting a local storage variable to store the proper value. The jist of it is I want to display the contents of the local variable, then if the user clicks, it pulls the data from an .xml file, saving it to a local variable.
我在获取本地存储变量来存储正确的值时遇到了一些麻烦。它的要点是我想显示局部变量的内容,然后如果用户单击,它会从 .xml 文件中提取数据,将其保存到局部变量中。
Problem is, that it doesn't save to the local variable properly. I have tried a variety of syntax to get it to work and I am out of ideas.
问题是,它没有正确保存到局部变量。我尝试了各种语法来让它工作,但我没有想法。
Test site for it is located at http://web.engr.oregonstate.edu/~todtm/assignment2.html
它的测试站点位于http://web.engr.oregonstate.edu/~todtm/assignment2.html
Script Code:
脚本代码:
function startAjax()
{
$("#clickme").text("Calling server");
$.ajax(
{
url: "xmlpage.xml",
success: callbackFunction,
error: errorFunction
});
}
function callbackFunction(data, info)
{
var titles = $(data).find("title");
if (titles && titles.length)
{
$("#results").text("result:" + titles.text());
localStorage.setItem('titles', #results.text());
}
else
errorFunction(data, "No titles");
}
function errorFunction(data, info)
{
$("#clickme").text("error occurred:" + info);
}
$(document).ready(function ()
{
$("#results").text(localStorage.getItem('titles'));
});
回答by Arun P Johny
you have a syntax error, need to get
你有一个语法错误,需要得到
localStorage.setItem('titles', $('#results').text());
or
或者
localStorage.setItem('titles', titles.text());