开发者

possible to replace window.location.hash?

开发者 https://www.devze.com 2023-01-09 21:39 出处:网络
I\'m wondering whether it\'s possible to change the hash in window.location.hash and replace it with \'this.id\'. Or would I need to change the entire window.lo开发者_开发百科cation?Yes, you can. I do

I'm wondering whether it's possible to change the hash in window.location.hash and replace it with 'this.id'. Or would I need to change the entire window.lo开发者_开发百科cation?


Yes, you can. I do something similar at one of my sites, although with href instead of id, but id works too. A quick example:

$('a[id]').click(function(e)
{
    // This will change the URL fragment. The change is reflected
    // on your browser's address bar as well
    window.location.hash = this.id;
    e.preventDefault();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消