aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/history/go/index.html
blob: a6fe7b38a84e5279d2024e8e3f983683237750b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: go()
slug: Web/API/History/go
tags:
  - History
translation_of: Web/API/History/go
---
<p><code>go()</code>方法从会话历史记录中加载特定页面。你可以使用它在历史记录中前后移动,具体取决于<code>delta</code>参数的值。</p>

<h2 id="语法">语法</h2>

<pre class="brush: js">window.history.go(delta);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>delta</code> {{optional_inline}}</dt>
 <dd>相对于当前页面你要去往历史页面的位置。负值表示向后移动,正值表示向前移动。因此,例如:<code>history.go(2)</code>向前移动两页,<code>history.go(-2)</code>则向后移动两页。如果未向该函数传参或<code>delta</code>相等于0,则该函数与调用<code>location.reload()</code>具有相同的效果。</dd>
 <dt>
 <div class="blockIndicator note">
 <p>译者注:</p>

 <p>相等于0是采用宽松相等进行比较的。另外,JavaScript值古怪的隐式转换在这里也是可用的。</p>
 </div>
 </dt>
</dl>

<h2 id="示例">示例</h2>

<p>向后移动一页(等价于调用<a href="https://developer.mozilla.org/en-US/docs/Web/API/History/back" title="The back method moves back one page in the session history. If there is no previous page, this method call does nothing."><code>back()</code></a>):</p>

<pre class="brush: js line-numbers language-js"><code class="language-js">window<span class="punctuation token">.</span>history<span class="punctuation token">.</span><span class="function token">go</span><span class="punctuation token">(</span><span class="operator token">-</span><span class="number token">1</span><span class="punctuation token">)</span></code></pre>

<p>向前移动一页,就像调用了<a href="https://developer.mozilla.org/en-US/docs/Web/API/History/forward" title="Moves forward one page in the session history. It has the same effect as calling history.go() with a delta parameter of 1."><code>forward()</code></a></p>

<pre class="brush: js line-numbers language-js"><code class="language-js">window<span class="punctuation token">.</span>history<span class="punctuation token">.</span><span class="function token">go</span><span class="punctuation token">(</span><span class="number token">1</span><span class="punctuation token">)</span></code></pre>

<p>向前移动两页:</p>

<pre class="brush: js">window.history.go(2);</pre>

<p>向后移动两页:</p>

<pre class="brush: js">window.history.go(-2);</pre>

<p>最后,以下任意一条语句都会重新加载当前页面:</p>

<pre class="brush: js">window.history.go();
window.history.go(0);</pre>

<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("HTML WHATWG", "browsers.html#history", "History")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>No change from {{SpecName("HTML5 W3C")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5 W3C", "browsers.html#history", "History")}}</td>
   <td>{{Spec2("HTML5 W3C")}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.History.go")}}</p>