codecamp

HTML事件属性onhashchange

HTML事件属性onhashchange


触发 onhashchange 属性事件当URL哈希改变时。

URL哈希是以当前URL的“#”符号开始的锚部分。

对于以下URL

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

此网址的锚定部分为 #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



HTML事件属性oncut
HTML事件属性onmouseup
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }