possible to replace window.location.hash?

醉酒当歌 提交于 2019-12-03 14:27:57

问题


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.location?


回答1:


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();
});


来源:https://stackoverflow.com/questions/3377150/possible-to-replace-window-location-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!