學習儲存Localstorage的程式方法

  • setItem(key, value) – store key/value pair.
  • getItem(key) – get the value by key.
  • removeItem(key) – remove the key with its value.
  • clear() – delete everything.
  • key(index) – get the key on a given position.
  • length – the number of stored items.

資料可以儲存成字串對應到數值

例如:

localStorage.setItem('text', text.value);
則會將資料儲存成key是text,值是text.value。

也可以儲存成陣列array的格式

 

HTML

See the Pen
JavaScript – store Localstorage
by G-Dee (@G-Dee)
on CodePen.


CSS

See the Pen
JavaScript – store Localstorage
by G-Dee (@G-Dee)
on CodePen.


JS

See the Pen
JavaScript – store Localstorage
by G-Dee (@G-Dee)
on CodePen.

 

參考資料:

影片

文章

最後修改日期: 2022 年 7 月 14 日

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。