web-dev-qa-db-ja.com

window.location.hashを置き換えることは可能ですか?

Window.location.hashのハッシュを変更して「this.id」に置き換えることができるかどうか疑問に思っています。または、window.location全体を変更する必要がありますか?

35
circey

はい、できます。 hrefではなくidを使用していますが、idも機能します。簡単な例:

$('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();
});
55
BoltClock