HTML事件屬性onhashchange

2018-01-05 13:58 更新

HTML事件屬性onhashchange


觸發(fā) onhashchange 屬性事件當URL哈希改變時。

URL哈希是以當前URL的“#”符號開始的錨部分。

對于以下URL

http://www.example.com/test.htm#myHash

此網(wǎng)址的錨定部分為 #myHash

我們可以通過設置 location.hash 來更改錨點散列或 Location 對象中的 location.href 屬性。

HTML5中的新功能

onhashchange 屬性是HTML5中的新特性。

句法

<element onhashchange="script or Javascript function name">

支持的標簽

<body>


瀏覽器兼容性

onhashchange Yes 8.0 Yes Yes Yes

例子

<!DOCTYPE html>
<html>
<body onhashchange="myFunction()">

<button onclick="changePart()">Click me to change the hash</button>

<script>
function changePart() {
    location.hash = "part5";
    alert("The anchor part is now: " + location.hash);
}
function myFunction() {
    alert("The anchor part has changed!");
}
</script>

</body>
</html>

Click to view the demo



以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號