可以替换window.location.hash?

cir*_*cey 34 javascript jquery window.location

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

Bol*_*ock 54

是的你可以.我在我的一个网站上做了类似的事情,虽然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();
});
Run Code Online (Sandbox Code Playgroud)

  • 我没有任何消息来源,除了我自己使用它的个人经验,这就是为什么我说我认为*所以. (2认同)