JQuery设置本地存储变量 [英] JQuery setting a local storage variable
问题描述
我在获取本地存储变量来存储适当的值时遇到了一些麻烦。它的特点是我想显示本地变量的内容,然后如果用户单击,它将从.xml文件中提取数据,并将其保存到本地变量中。
问题是,它不能正确保存到本地变量中。我尝试了各种语法来使它起作用,并且我没有想法。
测试网站位于 http://web.engr.oregonstate.edu/~todtm/assignment2.html
脚本代码:
function startAjax()
{
$(#clickme)。text(Calling server);
$ .ajax(
{
url:xmlpage.xml,
success:callbackFunction,
error:errorFunction
});
函数callbackFunction(data,info)
{
var titles = $(data).find(title); (title& title.length)
{
$(#results)。text(result:+ titles.text());
localStorage.setItem('titles',#results.text());
}
else
errorFunction(data,No titles);
函数errorFunction(data,info)
{
$(#clickme)。text(error occurred:+ info);
$ b $(document).ready(function()
{
$(#results)。text(localStorage.getItem('titles')) );
});
你有一个语法错误, p>
localStorage.setItem('titles',$('#results')。text());
或
localStorage.setItem('titles',titles.text());
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.
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
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'));
});
you have a syntax error, need to get
localStorage.setItem('titles', $('#results').text());
or
localStorage.setItem('titles', titles.text());
这篇关于JQuery设置本地存储变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!