开发者社区 问答 正文

localStorage存储总是出现undefined,求解

<script>
    function set(Key) {
        localStorage.setItem(Key, "demo");
    }

    function get(Key){
            localStorage.getItem(Key);
    }
</script>

上边是我的代码,但是我在输入set(1)的时候总是伴随undefined,但是却存入了,get(1)的时候也是一样出现undefined,但是取不到值,只有输入localStorage.getItem(1)的时候才能取到,有人知道为什么吗?求帮助
screenshot

展开
收起
小旋风柴进 2016-03-26 11:29:03 5035 分享 版权
1 条回答
写回答
取消 提交回答
  • 你函数没返回值当然是undefined

    function set(Key) {
            return localStorage.setItem(Key, "demo");
        }
    
        function get(Key){
            return localStorage.getItem(Key);
        }
    2019-07-17 19:15:54
    赞同 展开评论
问答分类:
问答地址: