我想知道是否可以更改window.location.hash中的哈希值并替换为'this.id'。还是说我需要更改整个window.location呢?
我想知道是否可以更改window.location.hash中的哈希值并替换为'this.id'。还是说我需要更改整个window.location呢?
可以的。我在我的网站上也做了类似的事情,虽然是使用 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();
});