localStorage pagecount - show / hid div

时间:2017-05-04 08:22:10

标签: javascript local-storage

我很高兴让这个工作起来。 根据访问我网站的数量,不要向人们显示特定的div。

var n = localStorage.getItem('on_load_count');
if (n === null) {
n = 0;
}
n++;
localStorage.setItem("on_load_count", n);

var v = localStorage.getItem('on_load_count'); 
if (v < "3" ) { 
document.querySelector('#myBox1').style.visibility = "visible";
document.querySelector('#myBox2').style.visibility = "hidden";
document.querySelector('#myBox3').style.visibility = "hidden";
} else if (v > "6" ) { 
document.querySelector('#myBox1').style.visibility = "hidden";
document.querySelector('#myBox2').style.visibility = "visible";
document.querySelector('#myBox3').style.visibility = "hidden";
} else if (v > "10" ) { 
document.querySelector('#myBox1').style.visibility = "hidden";
document.querySelector('#myBox2').style.visibility = "hidden";
document.querySelector('#myBox3').style.visibility = "visible";
} else { 
}

任何帮助表示赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:0)

将字符串更改为整数(v < 3 )>无法使用字符串

var n = localStorage.getItem('on_load_count');
if (n === null) {
n = 0;
}
n++;
localStorage.setItem("on_load_count", n);

var v = localStorage.getItem('on_load_count'); 
if (v < 3 ) { 
document.querySelector('#myBox1').style.visibility = "visible";
document.querySelector('#myBox2').style.visibility = "hidden";
document.querySelector('#myBox3').style.visibility = "hidden";
} else if (v > 6 ) { 
document.querySelector('#myBox1').style.visibility = "hidden";
document.querySelector('#myBox2').style.visibility = "visible";
document.querySelector('#myBox3').style.visibility = "hidden";
} else if (v > 10 ) { 
document.querySelector('#myBox1').style.visibility = "hidden";
document.querySelector('#myBox2').style.visibility = "hidden";
document.querySelector('#myBox3').style.visibility = "visible";
} else { 
}

答案 1 :(得分:0)

更改if条件的顺序,大不计数首先进行比较

if (v < 3 ) { 
document.querySelector('#myBox1').style.visibility = "visible";
document.querySelector('#myBox2').style.visibility = "hidden";
document.querySelector('#myBox3').style.visibility = "hidden";

 } else if (v > 10 ) { 
document.querySelector('#myBox1').style.visibility = "hidden";
document.querySelector('#myBox2').style.visibility = "hidden";
document.querySelector('#myBox3').style.visibility = "visible";

} else if (v > 6 ) { 
document.querySelector('#myBox1').style.visibility = "hidden";
document.querySelector('#myBox2').style.visibility = "visible";
document.querySelector('#myBox3').style.visibility = "hidden";
else { 
}