35

我想知道是否可以更改 window.location.hash 中的哈希并将其替换为“this.id”。还是我需要更改整个 window.location?

4

1 回答 1

56

是的你可以。我在我的一个站点上做了类似的事情,虽然用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();
});
于 2010-07-31T06:55:51.267 回答