Javascript 如何在本地存储中保存此按钮状态?
我有一个按钮,静音我的页面中的所有声音,我有其他取消静音它Javascript 如何在本地存储中保存此按钮状态?,javascript,jquery,html,Javascript,Jquery,Html,我有一个按钮,静音我的页面中的所有声音,我有其他取消静音它 $(document).ready(function(){ /*** Mute all ***/ $('#mute_all').on('click',function(){ /*** Mute all video and audio on page ***/ $('body video, body audio').each(function(){ /*** Do it here
$(document).ready(function(){
/*** Mute all ***/
$('#mute_all').on('click',function(){
/*** Mute all video and audio on page ***/
$('body video, body audio').each(function(){
/*** Do it here globally ***/
$(this).prop('muted', true);
});
});
/*** UnMute all ***/
$('#unmute_all').on('click',function(){
/*** Un Mute all video and audio on page ***/
$('body video, body audio').each(function(){
/*** Do it here globally ***/
$(this).prop('muted', false);
});
});
});
问题是当我刷新页面时,更改不会持久。我如何使用LocalStorage实现这一点
谢谢 每次用户单击静音/取消静音按钮时,使用
localStorage.setItem(“名称”、“值”)
将其选择保存在localStorage
变量中。只能保存字符串值
现在,在每次页面加载时,您应该首先使用localStorage.getItem(“name”)
检查该变量在用户的localStorage
中是否具有特定值,并相应地进行配置
每次用户单击静音/取消静音按钮时,使用
localStorage.setItem(“名称”、“值”)
将其选择保存在localStorage
变量中。只能保存字符串值
现在,在每次页面加载时,您应该首先使用localStorage.getItem(“name”)
检查该变量在用户的localStorage
中是否具有特定值,并相应地进行配置
单击“全部禁用”按钮,可以将禁用状态存储在本地存储器中。并在document ready函数中检查其初始值
$(document).ready(function(){
function muteAll(){
$('body video, body audio').each(function(){
$(this).prop('muted', true);
});
window.localStorage.setItem('muted','true');
}
function unmuteAll(){
$('body video, body audio').each(function(){
$(this).prop('muted', false);
});
window.localStorage.setItem('muted','false');
}
$('#mute_all').on('click',mute_all);
$('#unmute_all').on('click',unmute_all);
var status=window.localStorage.getItem('muted')
if(status && status=='true'){
mute_all();
}else{
unmute_all(); //if required
}
});
单击“全部禁用”按钮,可以将禁用状态存储在本地存储器中。并在document ready函数中检查其初始值
$(document).ready(function(){
function muteAll(){
$('body video, body audio').each(function(){
$(this).prop('muted', true);
});
window.localStorage.setItem('muted','true');
}
function unmuteAll(){
$('body video, body audio').each(function(){
$(this).prop('muted', false);
});
window.localStorage.setItem('muted','false');
}
$('#mute_all').on('click',mute_all);
$('#unmute_all').on('click',unmute_all);
var status=window.localStorage.getItem('muted')
if(status && status=='true'){
mute_all();
}else{
unmute_all(); //if required
}
});
1) 像这样设置项目
$('#mute_all').on('click',function(){
$('body video, body audio').each(function(){
/*** Do it here globally ***/
$(this).prop('muted', true);
});
localStorage.setItem("muted", "true")
});
localStorage.getItem("muted")
2) 得到这样的物品
$('#mute_all').on('click',function(){
$('body video, body audio').each(function(){
/*** Do it here globally ***/
$(this).prop('muted', true);
});
localStorage.setItem("muted", "true")
});
localStorage.getItem("muted")
3) 在document ready函数中,检查本地存储并使用click()单击元素
1) 像这样设置项目
$('#mute_all').on('click',function(){
$('body video, body audio').each(function(){
/*** Do it here globally ***/
$(this).prop('muted', true);
});
localStorage.setItem("muted", "true")
});
localStorage.getItem("muted")
2) 得到这样的物品
$('#mute_all').on('click',function(){
$('body video, body audio').each(function(){
/*** Do it here globally ***/
$(this).prop('muted', true);
});
localStorage.setItem("muted", "true")
});
localStorage.getItem("muted")
3) 在document ready函数中,检查本地存储并使用click()单击元素
使用document.cookie您是否尝试过
localStorage.setItem(“名称”、“值”)
和localStorage.getItem(“名称”)
?如果你问我,我肯定谷歌会告诉你的!我试过了!抱歉,如果我没有在问题Use document.cookie中说,您尝试了localStorage.setItem(“名称”、“值”)
和localStorage.getItem(“名称”)
?如果你问我,我肯定谷歌会告诉你的!我试过了!抱歉,如果我没有在问题中说,您应该使用window.localStorage.setItem('muted','false')代码>语句脱离每个循环。您应该使用窗口.localStorage.setItem('muted','false')每个循环外的code>语句。