html历史搜索记录,javascript-HTML中带有localStorage变量的搜索历史记录

本文介绍了如何在PhoneGap应用中使用jQuery Mobile实现搜索历史功能,包括存储和检索localStorage中的数组,以及在HTML列表中显示历史搜索结果。作者详细展示了如何操作localStorage,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在使用PhoneGap创建一个jQuery移动应用程序,并且我想列出旧的搜索结果(由表单输入并存储在localStorage中).

有两个不同的问题要解决:

1)我会将结果存储在localStorage数组中,如果用户第二次搜索,则应将结果添加到旧结果之后的数组中,例如:city [0] =“ New York”,city [1] = “巴黎” …如何保存和读取数组中的字符串,例如:

localStorage.setItem('city[i]', $('#city').val());

or

localStorage.getItem('city[i]');

2)现在我想显示搜索历史.我已经尝试过了,但是:

>我不知道如何在html列表中显示localStorage数组或变量,并且…

>如果localStorage中没有变量,则不会加载网站.

var history = "";

if (localStorage.getItem("history") !== "") {

var history = localStorage.getItem("history");

}

if ( history !== "") {

$("#lastResults").html(

"Last Results:" +

"

  • " +

"

" + document.write(history) + " " +

"

"

);

}

解决方法:

//To Check and show previous results in **lastResults** div

if (localStorage.getItem("history") != null)

{

var historyTmp = localStorage.getItem("history");

var oldhistoryarray = historyTmp.split('|');

$('#lastResults').empty();

for(var i =0; i

{

$('#lastResults').append('

'+oldhistoryarray[i]+'

');

}

}

//Storing New result in previous History localstorage

if (localStorage.getItem("history") != null)

{

var historyTmp = localStorage.getItem("history");

historyTmp += '|Paris|Pakistan|China|US';

localStorage.setItem("history",historyTmp);

}

else

{

var historyTmp = 'Paris|Pakistan|China|US';

localStorage.setItem("history",historyTmp);

}

注意我已经使用jquery来缩短代码.

标签:html5,cordova,html,javascript,jquery

来源: https://codeday.me/bug/20191030/1964812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值