假设我的HTML是
<a href="#one">One</a>
<a href="#two">Two</a>
Run Code Online (Sandbox Code Playgroud)
和Js是
$(window).on("hashchange"){
alert(document.location.hash);
}
Run Code Online (Sandbox Code Playgroud)
我想获得哈希值更改之前的哈希值.这可能吗?如果是,那怎么样?
Ami*_*mit 12
用那个
$(window).on("hashchange", function(e){
console.log(e.originalEvent.oldURL)
console.log(e.originalEvent.newURL)
})?;
Run Code Online (Sandbox Code Playgroud)
您必须跟踪最后一个哈希,例如:
var currentHash = function() {
return location.hash.replace(/^#/, '')
}
var last_hash
var hash = currentHash()
$(window).bind('hashchange', function(event){
last_hash = hash
hash = currentHash()
console.log('hash changed from ' + last_hash + ' to ' + hash)
});
Run Code Online (Sandbox Code Playgroud)