觸發(fā) onhashchange
屬性事件當URL哈希改變時。
URL哈希是以當前URL的“#”符號開始的錨部分。
對于以下URL
http://www.example.com/test.htm#myHash
此網(wǎng)址的錨定部分為 #myHash
。
我們可以通過設置 location.hash
來更改錨點散列或 Location
對象中的 location.href
屬性。
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>
更多建議: