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)的时候才能取到,有人知道为什么吗?求帮助

图片描述

阅读 14.1k
1 个回答

你函数没返回值当然是undefined

function set(Key) {
        return localStorage.setItem(Key, "demo");
    }

    function get(Key){
        return localStorage.getItem(Key);
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题