Storage.setItem()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Storage
接口的 setItem()
方法,当传递了一个键名和值时,将会把键名添加到给定的 Storage
对象中,如果键名已存在,则更新其对应的值。
语法
js
setItem(keyName, keyValue)
参数
返回值
无 undefined
。
异常
在存储已满的情况下,调用 setItem()
可能会抛出异常。开发人员应始终捕获 setItem()
可能抛出的异常。
示例
下面的函数在本地存储中创建三个数据项。
js
function populateStorage() {
localStorage.setItem("bgcolor", "red");
localStorage.setItem("font", "Helvetica");
localStorage.setItem("image", "myCat.png");
}
备注: 有关实际运行的例子,详见 Web Storage 演示。
规范
Specification |
---|
HTML # dom-storage-setitem-dev |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
setItem |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.