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>语句。