Javascript 检查localStorage是否等于value,隐藏元素
这是我的第二个问题,所以我希望它能解决所有问题。我不太懂javascript,因此无法准确地使我的代码正常工作。我尝试了许多不同的代码,但无法修复它。括号内的所有内容都是我想要的,但无法实现。我希望这一切发生在总结: -如果按下按钮1:将localStorage.thing1设置为0 -如果按下按钮2:将localStorage.thing1设置为1 -如果localStorage.thing1==0,则隐藏thingtext1。否则,显示内容文本1Javascript 检查localStorage是否等于value,隐藏元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,这是我的第二个问题,所以我希望它能解决所有问题。我不太懂javascript,因此无法准确地使我的代码正常工作。我尝试了许多不同的代码,但无法修复它。括号内的所有内容都是我想要的,但无法实现。我希望这一切发生在总结: -如果按下按钮1:将localStorage.thing1设置为0 -如果按下按钮2:将localStorage.thing1设置为1 -如果localStorage.thing1==0,则隐藏thingtext1。否则,显示内容文本1 这里有一些文本 隐藏 显示 (空载) if
这里有一些文本
隐藏
显示
(空载)
if(localStorage.getItem(“thing1”)=“0”){
(隐藏(#thingtext1))
}
否则{
if(localStorage.getItem(“thing1”)=“1”){
(显示内容文本1)
}
}
(将thing1设置为localStorage)
如果你能帮助我。
谢谢。这里有一些文字
<p id="thingtext1">Here Is Some Text</p>
<button id="thing1" onclick="storeLS('0')">Hide</button>
<button id="thing1" onclick="storeLS('1')">Show</button>
<script type="text/javascript">
function storeLS(num){
localStorage.setItem("thing1", num);
showRhide();
}
function showRhide(){
if (localStorage.getItem("thing1") == "0") {
$("#thingtext1").hide();
}else if (localStorage.getItem("thing1") == "1"){
$("#thingtext1").show();
}
}
$(document).ready(function(){
showRhide();
});
</script>
隐藏
显示
函数库(num){
setItem(“thing1”,num);
showRhide();
}
函数showRhide(){
if(localStorage.getItem(“thing1”)=“0”){
$(“#thingtext1”).hide();
}else if(localStorage.getItem(“thing1”)=“1”){
$(“#thingtext1”).show();
}
}
$(文档).ready(函数(){
showRhide();
});
尝试执行以下操作:
<p id="thingtext1">Here Is Some Text</p>
<button id="thing1" onClick="localStorage.setItem('thing1', '0')">Hide</button>
<button id="thing1" onClick="localStorage.setItem('thing1', '1')">Show</button>
<script>
$(function () {
$(window).bind('storage', function (e) {
if (localStorage.getItem("thing1") == "0") {
$("#thingtext1").hide();
}else if (localStorage.getItem("thing1") == "1"){
$("#thingtext1").show();
}
});
});
</script>
将在LocalStorage的值更改时执行。尝试搜索internet:我知道如何获取和设置LocalStorage项,但我不知道如何正确使用if语句我对代码不太熟悉,因此请不要太讨厌我的问题我尝试了此方法,但不起作用:(
hi
隐藏显示函数库(num){localStorage.setItem(“thing1”,num);}$(document.ready(function(){d if(localStorage.getItem(“thing1”)==“0”){$(“#thingtext1”).hide();}否则if(localStorage.getItem(“thing1”)==“1”){$(#thingtext1”).show()})“LINKED JQUERY 3.0.0 SCRIPT”效果很好!但是在刷新页面后,元素返回到默认状态(show)。如果您可以让localStorage保存项目的状态…显示或隐藏。那太好了!谢谢查看$(document)。ready函数。我包括了“showRhide()”谢谢你的帮助,但我想我知道的代码还不够多,无法让它正常工作。我只熟悉HTML5和CSS3。显示和隐藏可以工作,但当我重新加载localStorage时,它是未定义的。我一直将它链接到:code.jquery.com/jquery-1.10.2.js“>…$(“#thingtext1”).toggle(localStorage.getItem(“thing1”)=“1”);
<p id="thingtext1">Here Is Some Text</p>
<button id="thing1" onClick="localStorage.setItem('thing1', '0')">Hide</button>
<button id="thing1" onClick="localStorage.setItem('thing1', '1')">Show</button>
<script>
$(function () {
$(window).bind('storage', function (e) {
if (localStorage.getItem("thing1") == "0") {
$("#thingtext1").hide();
}else if (localStorage.getItem("thing1") == "1"){
$("#thingtext1").show();
}
});
});
</script>
$(window).bind('storage', function (e) {