31
我想知道是否有可能在window.location.hash中更改散列並將其替換爲'this.id'。或者我需要改變整個window.location?可能取代window.location.hash?
我想知道是否有可能在window.location.hash中更改散列並將其替換爲'this.id'。或者我需要改變整個window.location?可能取代window.location.hash?
是的,你可以。我在我的一個網站上做了類似的事情,雖然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();
});
很好:-)非常感謝! – circey 2010-07-31 07:07:30
這是跨瀏覽器嗎? – jldupont 2013-04-05 17:17:22
@jldupont:我應該這樣想。 – BoltClock 2013-04-05 17:18:12