可以替换window.location.hash? [英] possible to replace window.location.hash?

查看:92
本文介绍了可以替换window.location.hash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

是的,你可以。我在我的一个网站上做了类似的事情,虽然使用 href 而不是 id ,但是 id 也有效。一个简单的例子:

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

这篇关于可以替换window.location.hash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆