javascript 清除本地存储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12448600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:13:39  来源:igfitidea点击:

Clear local storage?

javascriptjquerylocal-storage

提问by Bergi

My online store uses local storage to store the cart. After the user has checked out, I want to clear it. However, localstorage.clear();isn't working for me.

我的在线商店使用本地存储来存储购物车。用户签出后,我想清除它。但是,localstorage.clear();对我不起作用。

 $(document).ready(function(){
    $("#matkahuolto").live('click', function() {
        $(".maksu").slideDown(600);
        $("#matkahuolto").attr("disabled" , "disabled");
        $("#posti").removeAttr("disabled");
        $("#matkahuolto").addClass( "selectedtoimitus" );
        $("#posti").removeClass( "selectedtoimitus" );
        $(".simpleCart_shipping").html("15.00");   
        localstorage.clear()


    });
 });

回答by Bergi

The correct spelling is localStorage- Javascript is case-sensitive:

正确的拼写是localStorage- Javascript 区分大小写:

localStorage.clear();

If this doesn't work because of a local variable, you'd need to use window.localStorage.

如果这由于局部变量而不起作用,则需要使用window.localStorage.

回答by NullPoiиteя

  localstorage.clear()
       ^
      here is the problem it must be in uppercase use below 


localStorage.clear();

or

或者

window.localStorage.clear()

回答by Niet the Dark Absol

It's localStorage- make sure you uppercase the S otherwise it doesn't refer to the same thing. JavaScript is case-sensitive.

它是localStorage- 确保您将 S 大写,否则它指的不是同一件事。JavaScript 区分大小写。

回答by Kev

Try:

尝试:

window.localStorage.clear()

The context of your handler may be affecting it.

您的处理程序的上下文可能会影响它。

回答by iOSAndroidWindowsMobileAppsDev

Using .one ensures this is done only once and not repeatedly.

使用 .one 可确保仅执行一次而不是重复执行。

    $(window).one("focus", function() {
        localStorage.clear();
    });

It is okay to put several document.ready event listeners (if you need other events to execute multiple times) as long as you do not overdo it, for the sake of readability.

可以放几个 document.ready 事件监听器(如果你需要其他事件多次执行),只要你不要过度,为了可读性。

.one is especially useful when you want local storage to be cleared only once the first time a web page is opened or when a mobile application is installed the first time.

当您希望仅在第一次打开网页或第一次安装移动应用程序时清除本地存储时,.one 尤其有用。

// Fired once when document is ready
 $(document).one('ready', function () {
   localStorage.clear();
 });