先说一下此段代码的主要功能:点击class=".pre"按钮时显示上一个楼宇,点击class=".next"是显示下一个楼宇,而localStorage则用来记载当前的楼宇。
问题:build顺序为:a,b,c,d,点击上一个(.pre)按钮时会按顺序实现,例如,如果当前是c,则点击上一个会依次显示b,a,所以上一个按钮是没有问题的。问题是下一个按钮(.next),如果当前是a,点击下一个就会直接跳到d,
现有如下代码:
nowStatID = 100000;
BuildingName = "中关村软件园A座";
boxid = "placeholder1";
var storage = window.localStorage;
if(storage.getItem("currentbuildingId")!= null)
{
nowStatID =storage.getItem("currentbuildingId");
BuildingName = storage.getItem("currentbuildingName");
}
$(document).ready(function(e) {
$("#buildingSummarize1 .next").click(function(){ //点击下一个
var build=eval(window.localStorage.getItem("buildings"));
for(var j=0; j<build.length; j++)
{
if(build[j].buildingID == nowStatID)
{
nowStatID = build[j+1].buildingID;
BuildingName = build[j+1].buildingName;
storage.setItem("currentbuildingId",nowStatID);
storage.setItem("currentbuildingName",BuildingName);
window.location.reload(); //(1)
}
}
});
$("#buildingSummarize1 .pre").click(function(){ //点击上一个
var build=eval(window.localStorage.getItem("buildings"));
for(var j=0; j<build.length; j++)
{
if(build[j].buildingID == nowStatID)
{
nowStatID = build[j-1].buildingID;
BuildingName = build[j-1].buildingName;
storage.setItem("currentbuildingId",nowStatID);
storage.setItem("currentbuildingName",BuildingName);
window.location.reload();
}
}
});
});
执行到(1)步骤时查看dom则发现localStorage存储的是a的下一个b,但是等到window.location.reload();之后显示的却是d,请问这是怎么回事呢?