diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/storage | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/storage')
-rw-r--r-- | files/zh-cn/web/api/storage/clear/index.html | 121 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/getitem/index.html | 131 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/index.html | 108 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/key/index.html | 124 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/length/index.html | 117 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/localstorage/index.html | 135 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/removeitem/index.html | 124 | ||||
-rw-r--r-- | files/zh-cn/web/api/storage/setitem/index.html | 72 |
8 files changed, 932 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/storage/clear/index.html b/files/zh-cn/web/api/storage/clear/index.html new file mode 100644 index 0000000000..02eab09559 --- /dev/null +++ b/files/zh-cn/web/api/storage/clear/index.html @@ -0,0 +1,121 @@ +--- +title: Storage.clear() +slug: Web/API/Storage/clear +translation_of: Web/API/Storage/clear +--- +<p>{{APIRef("Web Storage API")}}</p> + +<p><code>clear()</code> 是 {{domxref("Storage")}} 接口的一个方法,调用它可以清空存储对象里所有的键值。</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js"><em>storage</em>.clear();</pre> + +<h3 id="参数">参数</h3> + +<p><em>无。</em></p> + +<h3 id="返回值">返回值</h3> + +<p><em>无。</em></p> + +<h2 id="示例">示例</h2> + +<p>下面的函数在本地存储里面创建三个数据项,然后使用 <code>clear()</code> 把它们全部移除。</p> + +<pre class="brush: js">function populateStorage() { + localStorage.setItem('bgcolor', 'red'); + localStorage.setItem('font', 'Helvetica'); + localStorage.setItem('image', 'myCat.png'); + + localStorage.clear(); +}</pre> + +<div class="note"> +<p><strong>备注:</strong>一个实际的例子 <a href="https://github.com/mdn/web-storage-demo">Web Storage Demo</a>。</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#dom-storage-clear', 'clear()')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>localStorage</td> + <td>4</td> + <td>3.5</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + <tr> + <td>sessionStorage</td> + <td>5</td> + <td>2</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>2.1</td> + <td>{{ CompatUnknown }}</td> + <td>8</td> + <td>11</td> + <td>iOS 3.2</td> + </tr> + </tbody> +</table> +</div> + +<p>各浏览器支持的 localStorage 和 sessionStorage 容量不同。测试页面:<a class="external" href="http://dev-test.nemikor.com/web-storage/support-test/" title="http://dev-test.nemikor.com/web-storage/support-test/">detailed rundown of all the storage capacities for various browsers</a>.</p> + +<div class="note"> +<p><strong>Note: </strong>since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<p><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></p> diff --git a/files/zh-cn/web/api/storage/getitem/index.html b/files/zh-cn/web/api/storage/getitem/index.html new file mode 100644 index 0000000000..11869d49e2 --- /dev/null +++ b/files/zh-cn/web/api/storage/getitem/index.html @@ -0,0 +1,131 @@ +--- +title: Storage.getItem() +slug: Web/API/Storage/getItem +translation_of: Web/API/Storage/getItem +--- +<p>{{APIRef("Web Storage API")}}</p> + +<p><code>getItem()</code> 作为 {{domxref("Storage")}} 接口的方法,接受一个键名(key name)作为参数,并返回对应键名的值(key's value)。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>aValue</em> = <em>storage</em>.getItem(<em>keyName</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><em><code>keyName</code></em></dt> + <dd>一个包含键名的 {{domxref("DOMString")}}。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{domxref("DOMString")}},键名对应的值。如果键名不存在于存储中,则返回 <code>null</code>。</p> + +<h2 id="示例">示例</h2> + +<p>下面的函数从本地存储中获取三个数据项,然后使用他们在页面上设置自定义样式:</p> + +<pre class="brush: js">function setStyles() { + var currentColor = localStorage.getItem('bgcolor'); + var currentFont = localStorage.getItem('font'); + var currentImage = localStorage.getItem('image'); + + document.getElementById('bgcolor').value = currentColor; + document.getElementById('font').value = currentFont; + document.getElementById('image').value = currentImage; + + htmlElem.style.backgroundColor = '#' + currentColor; + pElem.style.fontFamily = currentFont; + imgElem.setAttribute('src', currentImage); +}</pre> + +<div class="note"> +<p><strong>备注:</strong>一个实际的例子 <a href="https://github.com/mdn/web-storage-demo">Web Storage Demo</a>。</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#dom-storage-getitem', 'getItem()')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>localStorage</td> + <td>4</td> + <td>3.5</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + <tr> + <td>sessionStorage</td> + <td>5</td> + <td>2</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>2.1</td> + <td>{{ CompatUnknown }}</td> + <td>8</td> + <td>11</td> + <td>iOS 3.2</td> + </tr> + </tbody> +</table> +</div> + +<p>各浏览器支持的 localStorage 和 sessionStorage 容量不同。测试页面:<a class="external" href="http://dev-test.nemikor.com/web-storage/support-test/" title="http://dev-test.nemikor.com/web-storage/support-test/">detailed rundown of all the storage capacities for various browsers</a>。</p> + +<div class="note"> +<p><strong>Note: </strong>since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<p><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></p> diff --git a/files/zh-cn/web/api/storage/index.html b/files/zh-cn/web/api/storage/index.html new file mode 100644 index 0000000000..d31bd6b056 --- /dev/null +++ b/files/zh-cn/web/api/storage/index.html @@ -0,0 +1,108 @@ +--- +title: Storage +slug: Web/API/Storage +tags: + - API + - Interface + - Reference + - Storage + - Web Storage + - data +translation_of: Web/API/Storage +--- +<p>{{APIRef("Web Storage API")}}</p> + +<p>作为 Web Storage API 的接口,<strong><code>Storage</code> </strong>提供了访问特定域名下的会话存储或本地存储的功能,例如,可以添加、修改或删除存储的数据项。</p> + +<p>如果你想要操作一个域名的会话存储,可以使用 {{domxref("Window.sessionStorage")}};如果想要操作一个域名的本地存储,可以使用 {{domxref("Window.localStorage")}}。</p> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("Storage.length")}} {{readonlyInline}}</dt> + <dd>返回一个整数,表示存储在 <code>Storage</code> 对象中的数据项数量。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("Storage.key()")}}</dt> + <dd>该方法接受一个数值 n 作为参数,并返回存储中的第 n 个键名。</dd> +</dl> + +<dl> + <dt>{{domxref("Storage.getItem()")}}</dt> + <dd>该方法接受一个键名作为参数,返回键名对应的值。</dd> + <dt>{{domxref("Storage.setItem()")}}</dt> + <dd>该方法接受一个键名和值作为参数,将会把键值对添加到存储中,如果键名存在,则更新其对应的值。</dd> + <dt>{{domxref("Storage.removeItem()")}}</dt> + <dd>该方法接受一个键名作为参数,并把该键名从存储中删除。</dd> + <dt>{{domxref("Storage.clear()")}}</dt> + <dd>调用该方法会清空存储中的所有键名。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>这里我们通过调用 <code>localStorage</code> 来访问一个 <code>Storage</code> 对象。首先,使用 <code>!localStorage.getItem('bgcolor')</code> 测试本地存储中是否包含该数据项。如果包含,则运行 <code>setStyles()</code> 函数,该函数使用 <code>localStorage.getItem()</code> 来获取数据项,并使用这些值更新页面样式。如果不包含,我们运行另一个函数 <code>populateStorage()</code>,该函数使用 <code>localStorage.setItem()</code> 设置数据项,然后运行 <code>setStyles()</code>。</p> + +<pre class="brush: js">if(!localStorage.getItem('bgcolor')) { + populateStorage(); +} else { + setStyles(); +} + +function populateStorage() { + localStorage.setItem('bgcolor', document.getElementById('bgcolor').value); + localStorage.setItem('font', document.getElementById('font').value); + localStorage.setItem('image', document.getElementById('image').value); + + setStyles(); +} + +function setStyles() { + var currentColor = localStorage.getItem('bgcolor'); + var currentFont = localStorage.getItem('font'); + var currentImage = localStorage.getItem('image'); + + document.getElementById('bgcolor').value = currentColor; + document.getElementById('font').value = currentFont; + document.getElementById('image').value = currentImage; + + htmlElem.style.backgroundColor = '#' + currentColor; + pElem.style.fontFamily = currentFont; + imgElem.setAttribute('src', currentImage); +}</pre> + +<div class="note"> +<p><strong>备注:</strong>要运行完整的例子,可查看 <a href="https://mdn.github.io/dom-examples/web-storage/">Web Storage Demo</a>。</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#the-storage-interface', 'Storage')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.Storage")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></li> + <li>{{domxref("Window.localStorage")}}</li> + <li>{{domxref("Window.sessionStorage")}}</li> + <li>{{domxref("CacheStorage")}}</li> +</ul> diff --git a/files/zh-cn/web/api/storage/key/index.html b/files/zh-cn/web/api/storage/key/index.html new file mode 100644 index 0000000000..bfd14f5c44 --- /dev/null +++ b/files/zh-cn/web/api/storage/key/index.html @@ -0,0 +1,124 @@ +--- +title: Storage.key() +slug: Web/API/Storage/key +translation_of: Web/API/Storage/key +--- +<p>{{APIRef()}}</p> + +<p><code>key()</code> 作为 {{domxref("Storage")}} 接口的方法,接受一个数值 n 作为参数,返回存储对象第 n 个数据项的键名。键的存储顺序是由用户代理定义的,所以尽可能不要依赖这个方法。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var <em>aKeyName</em> = <em>storage</em>.key(<em>key</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><em>key</em></dt> + <dd>一个整数,表示要获取的键名索引。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个包含键名的 {{domxref("DOMString")}}。</p> + +<h2 id="示例">示例</h2> + +<p>下面的函数添加三个数据项到当前域名的本地存储里,然后返回第三个的键名:</p> + +<pre class="brush: js notranslate">function populateStorage() { + localStorage.setItem('bgcolor', 'yellow'); + localStorage.setItem('font', 'Helvetica'); + localStorage.setItem('image', 'cats.png'); + + localStorage.key(2); // 应该返回 'image' +}</pre> + +<div class="note"> +<p><strong>备注:</strong> 关于实际的例子,可以查看 <a href="https://github.com/mdn/dom-examples/tree/master/web-storage">Web Storage Demo</a>.</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#dom-storage-key', 'key()')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>localStorage</td> + <td>4</td> + <td>3.5</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + <tr> + <td>sessionStorage</td> + <td>5</td> + <td>2</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>2.1</td> + <td>{{ CompatUnknown }}</td> + <td>8</td> + <td>11</td> + <td>iOS 3.2</td> + </tr> + </tbody> +</table> +</div> + +<p>各浏览器支持的 localStorage 和 sessionStorage 容量不同。测试页面:<a class="external" href="http://dev-test.nemikor.com/web-storage/support-test/" title="http://dev-test.nemikor.com/web-storage/support-test/">detailed rundown of all the storage capacities for various browsers</a>.</p> + +<div class="note"> +<p><strong>Note: </strong>since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<p><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></p> diff --git a/files/zh-cn/web/api/storage/length/index.html b/files/zh-cn/web/api/storage/length/index.html new file mode 100644 index 0000000000..5c4d79d3d3 --- /dev/null +++ b/files/zh-cn/web/api/storage/length/index.html @@ -0,0 +1,117 @@ +--- +title: Storage.length +slug: Web/API/Storage/length +translation_of: Web/API/Storage/length +--- +<p>{{APIRef("Web Storage API")}}</p> + +<p><code>length</code> 是 {{domxref("Storage")}} 接口的只读属性,返回一个整数,表示存储在 <code>Storage</code> 对象里的数据项(data items)数量。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>aLength</em> = <em>storage</em>.length;</pre> + +<h3 id="返回值">返回值</h3> + +<p>一个整数。</p> + +<h2 id="示例">示例</h2> + +<p>下面的函数添加三个数据项到当前域名的本地存储里面,然后返回本地存储里面数据项的数量:</p> + +<pre class="brush: js">function populateStorage() { + localStorage.setItem('bgcolor', 'yellow'); + localStorage.setItem('font', 'Helvetica'); + localStorage.setItem('image', 'cats.png'); + + localStorage.length; // 返回 3 +}</pre> + +<div class="note"> +<p><strong>备注:</strong> 关于实际的例子,可以查看 <a href="https://github.com/mdn/web-storage-demo">Web Storage Demo</a>.</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#dom-storage-length', 'length')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>localStorage</td> + <td>4</td> + <td>3.5</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + <tr> + <td>sessionStorage</td> + <td>5</td> + <td>2</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>2.1</td> + <td>{{ CompatUnknown }}</td> + <td>8</td> + <td>11</td> + <td>iOS 3.2</td> + </tr> + </tbody> +</table> +</div> + +<p>各浏览器支持的 localStorage 和 sessionStorage 容量不同。测试页面:<a class="external" href="http://dev-test.nemikor.com/web-storage/support-test/" title="http://dev-test.nemikor.com/web-storage/support-test/">detailed rundown of all the storage capacities for various browsers</a>.</p> + +<div class="note"> +<p><strong>Note: </strong>since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<p><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></p> diff --git a/files/zh-cn/web/api/storage/localstorage/index.html b/files/zh-cn/web/api/storage/localstorage/index.html new file mode 100644 index 0000000000..5d655432b9 --- /dev/null +++ b/files/zh-cn/web/api/storage/localstorage/index.html @@ -0,0 +1,135 @@ +--- +title: LocalStorage +slug: Web/API/Storage/LocalStorage +tags: + - 存储API + - 离线 +translation_of: Web/API/Window/localStorage +--- +<p><code>localStorage</code> 与<code> <a href="/en-US/docs/Web/API/sessionStorage">sessionStorage</a></code> 一样,都遵循同源策略,但是它是持续存在的。<code>localStorage</code> 首次出现于 Firefox 3.5。</p> + +<div class="note"><strong>Note:</strong> 当浏览器进入隐私浏览模式,会创建一个新的、临时的数据库来存储local storage的数据;当关闭隐私浏览模式时,该数据库将被清空并丢弃。</div> + +<pre class="brush:js" style="font-size: 14px;">// Save data to the current local store +localStorage.setItem("username", "John"); + +// Access some stored data +alert( "username = " + localStorage.getItem("username"));</pre> + +<p class="note">本地存储(<code>localStorage</code>)的持续存在对许多事情都有帮助,包括这个示例<a href="http://codepen.io/awesom3/pen/Hlfma">this tutorial on Codepen</a>所演示的记录页面查看次数。</p> + +<h4 id="兼容性" style="line-height: 18px; font-size: 1.28571428571429rem;">兼容性</h4> + +<p><code>Storage</code> 对象最近才加入标准,因此可能并不被所有浏览器支持。你可以通过在你的scripts代码前加入以下两段代码中某一段来规避在不能原生支持的执行环境使用<code>localStorage</code>对象的问题。</p> + +<p>该算法借助于cookies,对localStorage对象进行了严谨的实现。</p> + +<pre class="brush: js" style="font-size: 14px;">if (!window.localStorage) { + Object.defineProperty(window, "localStorage", new (function () { + var aKeys = [], oStorage = {}; + Object.defineProperty(oStorage, "getItem", { + value: function (sKey) { return sKey ? this[sKey] : null; }, + writable: false, + configurable: false, + enumerable: false + }); + Object.defineProperty(oStorage, "key", { + value: function (nKeyId) { return aKeys[nKeyId]; }, + writable: false, + configurable: false, + enumerable: false + }); + Object.defineProperty(oStorage, "setItem", { + value: function (sKey, sValue) { + if(!sKey) { return; } + document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"; + }, + writable: false, + configurable: false, + enumerable: false + }); + Object.defineProperty(oStorage, "length", { + get: function () { return aKeys.length; }, + configurable: false, + enumerable: false + }); + Object.defineProperty(oStorage, "removeItem", { + value: function (sKey) { + if(!sKey) { return; } + document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"; + }, + writable: false, + configurable: false, + enumerable: false + }); + this.get = function () { + var iThisIndx; + for (var sKey in oStorage) { + iThisIndx = aKeys.indexOf(sKey); + if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); } + else { aKeys.splice(iThisIndx, 1); } + delete oStorage[sKey]; + } + for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); } + for (var aCouple, iKey, nIdx = 0, aCouples = document.cookie.split(/\s*;\s*/); nIdx < aCouples.length; nIdx++) { + aCouple = aCouples[nIdx].split(/\s*=\s*/); + if (aCouple.length > 1) { + oStorage[iKey = unescape(aCouple[0])] = unescape(aCouple[1]); + aKeys.push(iKey); + } + } + return oStorage; + }; + this.configurable = false; + this.enumerable = true; + })()); +} +</pre> + +<div class="note"><strong>注意:</strong>数据的最大大小是通过cookies来严格限制的。可以使用这样的算法实现,使用<code>localStorage.setItem()</code>和<code>localStorage.removeItem()</code>这两个函数进行添加、改变或删除一个键。使用方法为<code>localStorage.yourKey = yourValue;和delete localStorage.yourKey;</code>进行设置和删除一个键<strong>并不是安全的做法</strong>。您也可以改变它的名字和使用它仅仅去管理文档的cookies而不管<code>localStorage </code>这个对象。</div> + +<div class="note"><strong>注意:</strong>通过将字符串<code style="background: rgb(204, 204, 204);">"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"</code> 改成<code style="background: rgb(204, 204, 204);">"; path=/"</code>(并且改变对象的名字),可以使得这个 <code>localStorage</code>的实现变为<code>sessionStorage</code>的实现。然而,这种实现方式会使储存键值可以跨标签和窗口访问(而且只会在浏览器窗口被关闭时销毁),完全兼容的<code>sessionStorage</code>实现会将储存键值访问权限限定在当前浏览上下文。</div> + +<p>下面是另一个,并不那么严谨的<code>localStorage</code>对象的实现。相比上一个方法,它更简单且能与旧的浏览器良好兼容,如Internet Explorer < 8 (甚至能在<strong> Internet Explorer 6 上正常工作</strong>)。它同样是使用cookies来实现的。</p> + +<pre class="brush:js" style="font-size: 14px;">if (!window.localStorage) { + window.localStorage = { + getItem: function (sKey) { + if (!sKey || !this.hasOwnProperty(sKey)) { return null; } + return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")); + }, + key: function (nKeyId) { + return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nKeyId]); + }, + setItem: function (sKey, sValue) { + if(!sKey) { return; } + document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"; + this.length = document.cookie.match(/\=/g).length; + }, + length: 0, + removeItem: function (sKey) { + if (!sKey || !this.hasOwnProperty(sKey)) { return; } + document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"; + this.length--; + }, + hasOwnProperty: function (sKey) { + return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); + } + }; + window.localStorage.length = (document.cookie.match(/\=/g) || window.localStorage).length; +} +</pre> + +<div class="note"><strong>注意:</strong>数据量的最大值是通过cookies来严格限制的。可以使用<code>localStorage.getItem()</code>, <code>localStorage.setItem()</code>和<code>localStorage.removeItem()</code>等方法获取、设置或删除一个键。使用方法<code>localStorage.yourKey = yourValue;</code>获取、添加、改变或者删除一个键<strong>并不是安全的做法</strong>。您也可以改变它的名字和使用它仅仅去管理文档的cookies而不管<code>localStorage </code>这个对象。</div> + +<div class="note"><strong>注意:</strong>通过将字符串<code style="background: rgb(204, 204, 204);">"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"</code> 改成<code style="background: rgb(204, 204, 204);">"; path=/"</code>(并且改变对象的名字),可以使得这个 <code>localStorage</code>的实现变为<code>sessionStorage</code>的实现。然而,这种实现方式会使储存键值可以跨标签和窗口访问(而且只会在浏览器窗口被关闭时销毁),完全兼容的sessionStorage实现会将储存键值限定在当前浏览环境上下文。</div> + +<h4 id="与globalStorage的兼容性及关系" style="line-height: 18px; font-size: 1.28571428571429rem;">与globalStorage的兼容性及关系</h4> + +<p class="note"><code>localStorage</code> 和 <code>globalStorage[location.hostname]</code> 是一样的, 除了作用域是在 HTML5 origin (结构 + 主机名 + 非标准的端口), 并且 <code>localStorage</code> 是一个 <code>Storage</code> 实例 ,而<code>globalStorage[location.hostname]</code> 是一个 <code>StorageObsolete</code> 实例,下面将要讨论这个。 例如, <a class="external" href="http://example.com" rel="freelink">http://example.com</a> 无法访问与 <a class="link-https" href="https://example.com" rel="freelink">https://example.com</a> 相同的 <code>localStorage</code> 对象 ,但是它们可以访问同一个 <code>globalStorage</code>. <code>localStorage</code> 是标准的接口,<code>globalStorage</code> 不是, 所以不要依赖于 <code>globalStorage</code> 。 </p> + +<p>请注意,给globalStorage[location.hostname]设置一个属性并不会影响到localStorage。拓展<code>Storage.prototype</code>也不会影响<code>globalStorage</code>对象,只有拓展<code>StorageObsolete.prototype</code>才会影响。</p> + +<h4 id="存储格式">存储格式</h4> + +<p><code>Storage</code>的键和值都是以每个字符占2个字节的UTF-16字符串(<a href="/zh-CN/docs/Web/API/DOMString">DOMString</a>)格式存储的。</p> diff --git a/files/zh-cn/web/api/storage/removeitem/index.html b/files/zh-cn/web/api/storage/removeitem/index.html new file mode 100644 index 0000000000..462f3cf703 --- /dev/null +++ b/files/zh-cn/web/api/storage/removeitem/index.html @@ -0,0 +1,124 @@ +--- +title: Storage.removeItem() +slug: Web/API/Storage/removeItem +translation_of: Web/API/Storage/removeItem +--- +<p>{{APIRef("Web Storage API")}}</p> + +<p>{{domxref("Storage")}} 接口的 <code>removeItem()</code> 方法,接受一个键名作为参数,会从给定的Storage对象中删除该键名(如果存在)。 如果没有与该给定键名匹配的项,则此方法将不执行任何操作。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>storage</em>.removeItem(<em>keyName</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><em><u>keyName</u></em></dt> + <dd>一个 {{domxref("DOMString")}},即你想要移除的键名。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><em>无。</em></p> + +<h2 id="示例">示例</h2> + +<p>下面的函数在本地存储里面创建三个数据项,然后把 <code>image</code> 数据项移除。</p> + +<pre class="brush: js">function populateStorage() { + localStorage.setItem('bgcolor', 'red'); + localStorage.setItem('font', 'Helvetica'); + localStorage.setItem('image', 'myCat.png'); + + localStorage.removeItem('image'); +}</pre> + +<div class="note"> +<p><strong>备注:</strong>完整的例子,可查看 <a href="https://github.com/mdn/web-storage-demo">Web Storage Demo</a>。</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#dom-storage-removeitem', 'removeItem()')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>localStorage</td> + <td>4</td> + <td>3.5</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + <tr> + <td>sessionStorage</td> + <td>5</td> + <td>2</td> + <td>8</td> + <td>10.50</td> + <td>4</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>2.1</td> + <td>{{ CompatUnknown }}</td> + <td>8</td> + <td>11</td> + <td>iOS 3.2</td> + </tr> + </tbody> +</table> +</div> + +<p>各浏览器支持的 localStorage 和 sessionStorage 大小上限不同。测试页面 <a class="external" href="http://dev-test.nemikor.com/web-storage/support-test/" title="http://dev-test.nemikor.com/web-storage/support-test/">detailed rundown of all the storage capacities for various browsers</a>。</p> + +<div class="note"> +<p><strong>Note: </strong>since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<p><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></p> diff --git a/files/zh-cn/web/api/storage/setitem/index.html b/files/zh-cn/web/api/storage/setitem/index.html new file mode 100644 index 0000000000..14cf70c89a --- /dev/null +++ b/files/zh-cn/web/api/storage/setitem/index.html @@ -0,0 +1,72 @@ +--- +title: Storage.setItem() +slug: Web/API/Storage/setItem +tags: + - API + - Storage + - Web Storage +translation_of: Web/API/Storage/setItem +--- +<p>{{APIRef("Web Storage API")}}</p> + +<p><strong><code>setItem()</code> </strong>作为 {{domxref("Storage")}} 接口的方法,接受一个键名和值作为参数,将会把键名添加到存储中,如果键名已存在,则更新其对应的值。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><em>storage</em>.setItem(<em>keyName</em>, <em>keyValue</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code><em>keyName</em></code></dt> + <dd>一个 {{domxref("DOMString")}},要创建或更新的键名。</dd> + <dt><code><em>keyValue</em></code></dt> + <dd>一个 {{domxref("DOMString")}},要创建或更新的键名对应的值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>{{jsxref("undefined")}}</p> + +<h2 id="示例">示例</h2> + +<p>下面的函数在本地存储中创建三个数据项。</p> + +<pre class="brush: js notranslate">function populateStorage() { + localStorage.setItem('bgcolor', 'red'); + localStorage.setItem('font', 'Helvetica'); + localStorage.setItem('image', 'myCat.png'); +}</pre> + +<div class="note"> +<p><strong>备注:</strong>一个实际的例子 <a href="https://mdn.github.io/dom-examples/web-storage/">Web Storage Demo</a>。</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Storage', '#dom-storage-setitem', 'setItem()')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.Storage.setItem")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem">Storage.getItem()</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem">Storage.removeItem()</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">Using the Web Storage API</a></li> +</ul> |