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/history/state/index.html | |
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/history/state/index.html')
-rw-r--r-- | files/zh-cn/web/api/history/state/index.html | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/history/state/index.html b/files/zh-cn/web/api/history/state/index.html new file mode 100644 index 0000000000..7b59ecb5eb --- /dev/null +++ b/files/zh-cn/web/api/history/state/index.html @@ -0,0 +1,69 @@ +--- +title: state +slug: Web/API/History/state +translation_of: Web/API/History/state +--- +<p>返回在 history 栈顶的 <code>任意</code> 值的拷贝。 通过这种方式可以查看 state 值,不必等待 <code><a href="https://developer.mozilla.org/en-US/docs/Web/Events/popstate" title="/en-US/docs/Web/Events/popstate">popstate</a></code>事件发生后再查看。</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">let currentState = history.state;</pre> + +<p>如果不进行下面两种类型的调用,state 的值将会是 null </p> + +<p>{{domxref("History.pushState","pushState()")}} or {{domxref("History.replaceState","replaceState()")}}.</p> + +<h2 id="例子">例子</h2> + +<p>下面 log 例句输出 history.state 的值,首先是在没有执行 {{domxref("History.pushState","pushState()")}} 语句进而将值 push 到 history 之前的 log。下面一行 log 语句再次输出 state 值,此时 history.state 已经有值。可以在脚本文件中执行下面语句,或者在控制台直接执行。</p> + +<pre class="brush: js">// 值为 null 因为我们还没有修改 history 栈 +console.log(`History.state before pushState: ${history.state}`); + +// 现在 push 一些数据到栈里 +history.replaceState({name: 'Example'}, "pushState example", 'page3.html'); + +// 现在 state 已经有值了 +console.log(`History.state after pushState: ${history.state}`);</pre> + +<h2 id="SpecificationsE">Specifications<a class="button section-edit only-icon" href="https://developer.mozilla.org/en-US/docs/Web/API/History$edit#Specifications" rel="nofollow, noindex"><span>E</span></a></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><a class="external external-icon" href="https://html.spec.whatwg.org/multipage/browsers.html#the-history-interface" hreflang="en" lang="en" rel="noopener">HTML Living Standard<br> + <small lang="en-US">The definition of 'History' in that specification.</small></a></td> + <td><span class="spec-Living">Living Standard</span></td> + <td>Adds the <code>scrollRestoration</code> attribute.</td> + </tr> + <tr> + <td><a class="external external-icon" href="https://www.w3.org/TR/html50/browsers.html#the-history-interface" hreflang="en" lang="en" rel="noopener">HTML5<br> + <small lang="en-US">The definition of 'History' in that specification.</small></a></td> + <td><span class="spec-REC">Recommendation</span></td> + <td>Initial definition.</td> + </tr> + <tr> + <td><a class="external external-icon" href="https://majido.github.io/scroll-restoration-proposal/history-based-api.html#web-idl" hreflang="en" lang="en" rel="noopener">Custom Scroll Restoration - History-based API<br> + <small lang="en-US">The definition of 'History' in that specification.</small></a></td> + <td><span class="spec-Draft">Draft</span></td> + <td>Adds the <code>scrollRestoration</code> attribute.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.History.state")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/History_API/Working_with_the_History_API">Working with the History API</a></li> +</ul> |