aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/window/hashchange_event/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/window/hashchange_event/index.html')
-rw-r--r--files/zh-cn/web/api/window/hashchange_event/index.html88
1 files changed, 88 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/window/hashchange_event/index.html b/files/zh-cn/web/api/window/hashchange_event/index.html
new file mode 100644
index 0000000000..de51f52ff8
--- /dev/null
+++ b/files/zh-cn/web/api/window/hashchange_event/index.html
@@ -0,0 +1,88 @@
+---
+title: 'Window: hashchange event'
+slug: Web/API/Window/hashchange_event
+tags:
+ - API
+ - DOM
+ - Event
+ - HashChange
+ - Window
+ - onhashchange
+ - 事件
+ - 参考
+translation_of: Web/API/Window/hashchange_event
+---
+<p>{{APIRef}}</p>
+
+<p>当URL的片段标识符更改时,将触发<strong>hashchange</strong>事件 (跟在#符号后面的URL部分,包括#符号)</p>
+
+<table class="properties">
+ <tbody>
+ <tr>
+ <th scope="row">Bubbles</th>
+ <td>Yes</td>
+ </tr>
+ <tr>
+ <th scope="row">Cancelable</th>
+ <td>No</td>
+ </tr>
+ <tr>
+ <th scope="row">Interface</th>
+ <td>{{domxref("HashChangeEvent")}}</td>
+ </tr>
+ <tr>
+ <th scope="row">Event handler</th>
+ <td>{{domxref("WindowEventHandlers/onhashchange", "onhashchange")}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="示例">示例</h2>
+
+<p>你可以在 {{domxref("EventTarget/addEventListener", "addEventListener")}} 方法中使用 <code>hashchange</code> 事件:</p>
+
+<pre class="brush:js">window.addEventListener('hashchange', function() {
+ console.log('The hash has changed!')
+}, false);</pre>
+
+<p>或使用 <code>onhashchange</code> 事件处理程序属性:</p>
+
+<pre class="brush: js">function locationHashChanged() {
+ if (location.hash === '#cool-feature') {
+ console.log("You're visiting a cool feature!");
+ }
+}
+
+window.onhashchange = locationHashChanged;</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">注释</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('HTML WHATWG', 'indices.html#event-hashchange', 'hashchange')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Window.hashchange_event")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{domxref("Window/popstate_event", "popstate")}}</li>
+ <li>{{domxref("WindowEventHandlers.onhashchange")}}</li>
+</ul>