74

ハッシュされた URL (document.location.hash) を replace メソッドで変更しようとしていますが、うまくいきません。

$(function(){
 var anchor = document.location.hash;
 //this returns me a string value like '#categories'

  $('span').click(function(){

     $(window).attr('url').replace(anchor,'#food');
     //try to change current url.hash '#categories'
     //with another string, I stucked here.
  });

});

ページを変更/更新したくありません。応答なしで URL を置き換えたいだけです。

注: href="#food" ソリューションでこれを解決したくありません。

4

2 に答える 2

138

後者の代わりにlocationorを使用することは非標準です。window.locationdocument.location

window.location.hash = '#food';

これにより、URL のハッシュが設定した値に置き換えられます。

参照

于 2012-07-21T00:28:30.547 に答える